Created
May 20, 2020 14:51
This file contains hidden or 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: "site@webmotors.com.br", | |
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