8

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?

1 Answer 1

13
#!/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"]
Sign up to request clarification or add additional context in comments.

4 Comments

I don't need to take OAuth::AccessToken since I already have oauth_secret and oauth_token.
@AlanDert I know. Use the object you get here: access_token=request_token.get_access_token(:oauth_verifier => verifier)
What is verifier? Can you give the complete example please? Be aware, that Typhoeus has an error, I can't use it.
How do I do that for oAuth2? Please show me again.I saw the examples at GitHub, but the constructor of AccessToken was different from the one in the first version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.