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

1

10

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!

Stace answered 12/1, 2011 at 21:22 Comment(2)
Of course, now I have figured it out just after posting. To benefit others who might be in a similar boat, the problem is that the Tumblr OAuth implementation is expecting the oauth_verifier key that it passes to the OAuth callback you supply. So, the code @access_token = @request_token.get_access_token needs to be changed to @access_token = @request_token.get_access_token({:oauth_verifier => params[:oauth_verifier]}).Stace
Wow, this helped me a lot. Tumblr should mention this on their API page, as I wasted a few good hours trying to figure out this exact issue.Ingunna
A
2

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
Amaranthine answered 14/4, 2011 at 11:56 Comment(1)
It gets stuck and not responding. Can you confirm please your code work for you?Oversubtle

© 2022 - 2024 — McMap. All rights reserved.