Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To make an API call with an oAuth token

Tags:

ruby

oauth

I want to make oAuth request in Ruby. I skimmed some examples but none of them used oauth_token_secret and oauth_token to make a request, they only used consumer_key and consumer_secret to get oauth_token_secret and oauth_token. But I already have oauth_token_secret and oauth_token.

For example, this one I tried to use

require 'rubygems'
require 'oauth'
consumer = OAuth::Consumer.new(consumer_key, consumer_secret,
                               {
                                 :site=> "https://www.google.com",
                                 :scheme=> :header,
                                 :http_method=> :post,
                                 :request_token_path => "/accounts/OAuthGetRequestToken",
                                 :access_token_path => "/accounts/OAuthGetAccessToken",
                                 :authorize_path=> "/accounts/OAuthAuthorizeToken",
                                 :signature_method=>"RSA-SHA1"},
                               # :private_key_file=>PATH_TO_PRIVATE_KEY
                               )

request_token = consumer.get_request_token()

puts "Visit the following URL, log in if you need to, and authorize the app"
puts request_token.authorize_url
puts "When you've authorized that token, enter the verifier code you are assigned:"

verifier = gets.strip

puts "Converting request token into access token..."

access_token=request_token.get_access_token(:oauth_verifier => verifier)

puts "access_token.token --> #{access_token.token}" # But I initially have it
puts "access_token.secret --> #{access_token.secret}" # But I initially have it

In my case, there are 4 secret keys:

consumer_key = "anonymous"
consumer_secret = "anonymous"
oauth_token_secret = "fdsfdsfdfdsfds"
oauth_token = "fdsfdsfdfdsfdsdsdsdsdsdsds"

So what I need to do is, to make a API request to the certain url with some additional get parameters and oAuth token and to get the answer.

How do I do that in Ruby?

like image 962
Alan Coromano Avatar asked Dec 11 '12 04:12

Alan Coromano


People also ask

How do I call API access token?

You need to perform the following: Register your app in the Security Token Service, based on IdentityServer3. Within your app, acquire an access token from the STS. Add an authorization header Bearer access_token and call the Sitefinity Web API.

What is API OAuth token?

API tokens are issued by users in an organization and are associated with the user's account and the organization from which they generated the API token. Once created by a user in an organization, OAuth apps act as entities in Server to server interactions and can be used in multiple organizations.

How do I call OAuth token API in Postman?

In Postman Under the Authorization tab, select OAuth 2.0 . Input the required information. Click Request Token and walk through the process to authorize access. Click Use Token to populate the Access Token field in Postman.


1 Answers

#!/usr/bin/env ruby
require 'rubygems'
require 'oauth'
require 'json'

You need to get your access_token (OAuth::AccessToken).

# Initialisation based on string values:
consumer_key = 'AVff2raXvhMUxFnif06g'
consumer_secret = 'u0zg77R1bQqbzutAusJYmTxqeUpWVt7U2TjWlzbVZkA'
access_token = 'R1bQqbzYm0zg77tAusJzbVZkAVt7U2T'
access_token_secret = 'sVbVZkAt7U2TjWlJYmTxqR1bQqbzutAuWzeUpu0zg77'

@consumer = OAuth::Consumer.new(consumer_key, consumer_secret, {:site=>'http://my.site'})
accesstoken = OAuth::AccessToken.new(@consumer, access_token, access_token_secret)

Once you have your OAuth::AccessToken object, you do :

json_response = accesstoken.get('/photos.xml')
# or
json_response = accesstoken.post(url, params_hash)

etc.

The response is a json object. To read it, you can do :

response = JSON.parse(json_response.body)
# which is a hash
# you just access content like
id = response["id"]
like image 61
oldergod Avatar answered Oct 02 '22 15:10

oldergod