Moolio
Moolio

Reputation: 253

Help Refreshing Yahoo's OAuth Access Token in Ruby

I'm at the point of involuntary hair loss while trying to refresh the Yahoo OAuth access token in Ruby.

Using the OmniAuth and OAuth gems, I'm able to get an access token from Yahoo, however it expires in one hour.

I'm following the Yahoo instructions to refresh an expired token, and am consistently returned a 401.

If someone could show me how to refresh the access token using the OAuth gem, I'd be greatly appreciative.

Upvotes: 4

Views: 1503

Answers (2)

olore
olore

Reputation: 4847

Note: oauth_session_handle is returned as a param by the call to get_access_token:

access_token         = request_token.get_access_token(:oauth_verifier => oauth_verifier)  
oauth_session_handle = access_token.params['oauth_session_handle']

This was less than obvious from looking at the oauth-ruby/oauth code

Upvotes: 1

ken-pepple
ken-pepple

Reputation: 56

First, make sure you are saving your oauth_session_handle parameter from your original get_access_token call.

Then, when you are looking to refresh the access_token do something like this:

request_token = OAuth::RequestToken.new(consumer, 
                                        config["ACCESS_TOKEN"],             
                                        config["ACCESS_TOKEN_SECRET"])
token = OAuth::Token.new(config["ACCESS_TOKEN"],
                         config["ACCESS_TOKEN_SECRET"])
@access_token = request_token.get_access_token(
                         :oauth_session_handle => config["SESSION_HANDLE"],
                         :token => token)  

... where ...

config["ACCESS_TOKEN"] is your old access token
config["ACCESS_TOKEN_SECRET"] is your old secret
config["SESSION_HANDLE"] is your oauth_session_handle
consumer is your OAuth::Consumer.new reference

I store the config variable in a yaml file and then load it on startup.

Remember to store the @access_token for next time.

I adapted this from an answer at YDN OAuth Forum.

Upvotes: 4

Related Questions