Skip to content
tpitale edited this page Aug 13, 2010 · 6 revisions

Session now accepts an OAuth access_token for authentication purposes. I won’t go into how this is done in a Rails or Sinatra application, but, to acquire an access_token in IRB or on the console you can step through the following code.

Create the consumer

consumer = OAuth::Consumer.new('anonymous', 'anonymous', {
  :site => 'https://www.google.com',
  :request_token_path => '/accounts/OAuthGetRequestToken',
  :access_token_path => '/accounts/OAuthGetAccessToken',
  :authorize_path => '/accounts/OAuthAuthorizeToken'
})

Note: using ‘anonymous’ for the consumer only works if you do not have a site to redirect back to. Do not use this outside of IRB.

Create the request token

request_token = consumer.get_request_token({}, {:scope => 'https://www.google.com/analytics/feeds'})

Authorize

Then, we can go to the authorization url on Google to login to our Analytics account:

puts request_token.authorize_url

Get Access Token

This will give you a code, paste that in for the oauth_verifier, below.
Finally, we can get the access token given the previous request token. Note: it must be the same request token!

access_token = request_token.get_access_token(:oauth_verifier => 'paste code from google here')

We should not need to re-request the access token, again. So, hang on to the user token and secret from the access_token:

puts access_token.token
puts access_token.secret

We can always rebuild our access token, given the original consumer, and the user token/secret we just displayed:

access_token = OAuth::AccessToken.new(consumer, token, secret)

Now that we’ve got our access token, let’s use it for authentication in Garb:

Use Access Token for Garb Authentication

Garb::Session.access_token = access_token
Clone this wiki locally