Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an Access Token with OAuth-Ruby and Tumblr API (Rails 3)

I am using OAuth-Ruby to do an OAuth authentication with a Tumblr application. I am able to write code that progresses through the various steps of OAuth, but I cannot get an access token or actually make a request. I can get a request key, redirect the user to Tumblr to authenticate and grant access, and receive an authenticated request key. But I can't get any farther than that.

I have registered my Tumblr application; let's assume for this question that it has provided me with the following keys:

  • OAuth Consumer Key: @oauth_consumer_key
  • Secret Key: @secret_key

(I have actual values, but I am keeping them concealed here for obvious reasons.)

I am running the following code within a controller that runs when the user submits a form, which form stores information in the @tumblog variable:

#0. provided when registering application
@key = @oauth_consumer_key
@secret = @secret_key
@site = 'http://www.tumblr.com'
@consumer = OAuth::Consumer.new(@key, @secret,
                               { :site => @site,
                                 :request_token_path => '/oauth/request_token',
                                 :authorize_path => '/oauth/authorize',
                                 :access_token_path => '/oauth/access_token',
                                 :http_method => :post } )
if @consumer
  #1. get a request token
  @request_token = @consumer.get_request_token;
  session[:request_token] = @request_token
  session[:tumblog] = @tumblog

  #2. have the user authorize
  redirect_to @request_token.authorize_url
else
  flash[:error] = "Failed to acquire request token from Tumblr."
  render 'new'
end

This code gets me to the right page at Tumblr, where the user grants or denies my application access to the user's account. Assuming the user grants access, Tumblr redirects back to my application, to a callback I provided when I registered the application with Tumblr. To that point, everything works beautifully.

My OAuth callback runs the following code in the controller:

if params[:oauth_token] && params[:oauth_verifier]
  @tumblog = session[:tumblog]
  @request_token = session[:request_token]

  #3. get an access token
  @access_token = @request_token.get_access_token

  . . . .
end

At Step 3, there is a problem. I cannot seem to actually get an access token with the line:

  @access_token = @request_token.get_access_token

Can someone tell me what I need to do to get the access token? When I run that line, I get a OAuth::Unauthorized error.

I truly appreciate any advice. I've been Googling and trying different things for multiple days. Thanks!

like image 679
Morris Singer Avatar asked Jan 12 '11 21:01

Morris Singer


1 Answers

i use Pelle's oauth plugin and modified it a little to support xauth like this :

require 'rubygems'
require 'oauth'

CONSUMER_KEY = 'YOUR_CONSUMER_KEY'
CONSUMER_SECRET = 'YOUR_CONSUMER_SECRET'

consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => 'https://www.tumblr.com/oauth/access_token')
access_token = consumer.get_access_token(nil, {}, { :x_auth_mode => 'client_auth', 
                                                    :x_auth_username => "[email protected]", 
                                                    :x_auth_password => "password"})
tumblr_credentials = access_token.get('http://www.tumblr.com/api/authenticate')

puts access_token
puts access_token.token
puts access_token.secret        
puts tumblr_credentials.body
like image 187
Goz Avatar answered Nov 05 '22 20:11

Goz