To call an API call with an oAuth token

I want to make an oAuth request in Ruby. I looked through a few examples, but none of them used oauth_token_secret and oauth_token to query, they 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, 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 an API request to a specific URL with some additional receive parameters and an oAuth token and get a response.

How to do it in Ruby?

+8
ruby oauth
source share
1 answer
 #!/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 an OAuth::AccessToken , follow these steps:

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

and etc.

The answer 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"] 
+12
source share

All Articles