Created
May 20, 2020 14:51
-
-
Save marciok/f08eb9661a9f674c99e45ef390b96a1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'http' | |
require 'base64' | |
# HTTP monkeypatch in onder to preserve the keys on the headers. | |
# This is to avoid client_id to become client-id (WRONG) | |
module DontNormalizeUnderscoreHeaders | |
def normalize_header(name) | |
return name if name.include?('_') | |
super | |
end | |
end | |
module HTTP | |
class Headers | |
prepend DontNormalizeUnderscoreHeaders | |
end | |
end | |
base_uri = 'https://hlg-webmotors.sensedia.com' | |
config = { | |
client_id: 'YOUR_CLIENT_ID', | |
client_secret: 'YOUR_CLIENT_SECRET' | |
} | |
client_and_secret = Base64.strict_encode64("#{config[:client_id]}:#{config[:client_secret]}") | |
response = HTTP.post( | |
"#{base_uri}/oauth/v1/access-token", | |
json: { | |
username: "[email protected]", | |
password: "teste123", | |
integracaosite: true, | |
grant_type: "password" | |
}, | |
headers: { | |
'Accept'=>'application/json, text/plain, */*', | |
'Content-Type'=>'application/json;charset=utf-8', | |
'Authorization' => "Basic #{client_and_secret}" | |
} | |
) | |
content = JSON.parse(response.body) | |
token = content['access_token'] | |
puts "Token: #{token}" | |
response = HTTP.get("#{base_uri}/site/v1/estoque", headers: { | |
'access_token' => token, | |
'client_id' => config[:client_id], | |
}) | |
puts response.body |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment