12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/unimatrix/authorization/client_credentials_grant.rb', line 12
def request_token
uri = URI.parse( "#{ Unimatrix.configuration.authorization_url }/token" )
params = { "grant_type" => "client_credentials" }
http = Net::HTTP.new( uri.host, uri.port )
request = Net::HTTP::Post.new( uri.request_uri )
http.use_ssl = true if uri.scheme == 'https'
request.basic_auth( @client_id, @client_secret )
request.set_form_data( params )
begin
response = http.request( request )
if response.code == '200'
body = JSON.parse( response.body )
body = body[ 'token' ] if body[ 'token' ].present?
body[ 'access_token' ] rescue nil
else
puts "ERROR: #{ response.body }"
end
rescue => e
puts "REQUEST FAILED: #{ e }"
end
end
|