Skip to content

Instantly share code, notes, and snippets.

@jotto
Created June 14, 2012 21:15
Show Gist options
  • Save jotto/2932998 to your computer and use it in GitHub Desktop.
Save jotto/2932998 to your computer and use it in GitHub Desktop.
ruby command line script for generating google oauth2 access token
# (create oauth2 tokens from Google Console)
client_id = ""
client_secret = ""
# (paste the scope of the service you want here)
# e.g.: https://www.googleapis.com/auth/gan
scope = ""
# gem install oauth2
require 'oauth2'
raise "Missing client_id variable" if client_id.to_s.empty?
raise "Missing client_secret variable" if client_secret.to_s.empty?
raise "Missing scope variable" if scope.to_s.empty?
redirect_uri = 'https://localhost/oauth2callback'
auth_client_obj = OAuth2::Client.new(client_id, client_secret, {:site => 'https://accounts.google.com', :authorize_url => "/o/oauth2/auth", :token_url => "/o/oauth2/token"})
# STEP 1
puts "1) Paste this URL into your browser where you are logged in to the relevant Google account\n\n"
puts auth_client_obj.auth_code.authorize_url(:scope => scope, :access_type => "offline", :redirect_uri => redirect_uri, :approval_prompt => 'force')
# STEP 2
puts "\n\n\n2) Accept the authorization request from Google in your browser:"
# STEP 3
puts "\n\n\n3) Google will redirect you to localhost, but just copy the code parameter out of the URL they redirect you to, paste it here and hit enter:\n"
code = gets.chomp.strip
access_token_obj = auth_client_obj.auth_code.get_token(code, { :redirect_uri => redirect_uri, :token_method => :post })
# STEP 4
puts "Token is: #{access_token_obj.token}"
puts "Refresh token is: #{access_token_obj.refresh_token}"
puts "\n\n\nNow you can make API requests with the following api_access_token_obj variable!\n"
puts "api_client_obj = OAuth2::Client.new(client_id, client_secret, {:site => 'https://www.googleapis.com'})"
puts "api_access_token_obj = OAuth2::AccessToken.new(api_client_obj, '#{access_token_obj.token}')"
puts "api_access_token_obj.get('some_relative_path_here') OR in your browser: http://www.googleapis.com/some_relative_path_here?access_token=#{access_token_obj.token}"
puts "\n\n... and when that access_token expires in 1 hour, use this to refresh it:\n"
puts "refresh_client_obj = OAuth2::Client.new(client_id, client_secret, {:site => 'https://accounts.google.com', :authorize_url => '/o/oauth2/auth', :token_url => '/o/oauth2/token'})"
puts "refresh_access_token_obj = OAuth2::AccessToken.new(refresh_client_obj, '#{access_token_obj.token}', {refresh_token: '#{access_token_obj.refresh_token}'})"
puts "refresh_access_token_obj.refresh!"
@edward
Copy link

edward commented Dec 17, 2012

When using a refresh token, you likely don’t care about the existing token, so get your new access token like this instead:

access_token = OAuth2::AccessToken.from_hash(client, :refresh_token => REFRESH_TOKEN).refresh!

Note that the assignment of access_token to itself is happening here because the expected in-place replacement of the access token that calling OAuth2::AccessToken#refresh! should provide doesn’t actually happen.

@benmwendwa
Copy link

Thanks a lot edward. Your answer helped me solve the problem with refreshing a token. Cheers

@NileshN
Copy link

NileshN commented Oct 6, 2017

Much Thanks. Very nicely explained and written. I was searching for this for couple of days. Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment