Created
August 27, 2012 22:28
-
-
Save atoulme/3492946 to your computer and use it in GitHub Desktop.
OAuth2 example to access Intalio|Create
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require "wrest" | |
require "multi_json" | |
require 'pp' | |
target_host="http://localhost:8080" | |
login_user="enter_username" | |
login_user_password="enter_password" | |
include Wrest | |
# First Touch | |
__first_touch = "#{target_host}/oauth/client".to_uri(:timeout => 50).get | |
client_id = __first_touch.headers['client-id'] | |
auth_host = __first_touch.headers['auth-host'] | |
puts "Client id #{client_id}" | |
puts "Auth host #{auth_host}" | |
# Login | |
## Get Auth | |
token_req = { | |
:client_id => client_id, | |
:username => login_user, | |
:password => login_user_password, | |
:grant_type => 'password', | |
}; | |
res2 = "#{auth_host}/auth/token".to_uri(:timeout => 50).post_form(token_req, {}) | |
puts "Auth response: #{res2.body.inspect}" | |
# | |
resp_json = MultiJson.decode(res2.body) | |
# | |
## note that parameters are case senstive and should be in lower case | |
## while headers are not | |
# This should be extracted from the Auth response | |
access_token = resp_json["access_token"] | |
puts "Access token #{access_token}" | |
unless access_token.nil? | |
access_req = { | |
:Authorization => 'OAuth', | |
:user => 'cheong', #login_user, # io_username of the user | |
'access-token' => access_token | |
} | |
# Now we make a request for the resource we need from the Platform | |
# Fill the headers of the request with the following key/values | |
# 1st arg in get is the http params, 2nd arg is http headers | |
# take your pick, it should work for both (header preferred though) | |
res3 = "#{target_host}".to_uri(:timeout => 500).get({}, access_req) | |
puts "Client id: #{res3['client-id']}" | |
puts "Auth host: #{res3['auth-host']}" | |
puts "Platform response: #{res3.body.inspect}" | |
else | |
puts "No access token" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment