Skip to content

Instantly share code, notes, and snippets.

@marciok
Created May 20, 2020 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marciok/f08eb9661a9f674c99e45ef390b96a1c to your computer and use it in GitHub Desktop.
Save marciok/f08eb9661a9f674c99e45ef390b96a1c to your computer and use it in GitHub Desktop.
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