Skip to content

Instantly share code, notes, and snippets.

@edds
Created Mar 5, 2014
Embed
What would you like to do?
Generate a Google api refresh token to authenticate API requests
# Pre-requisites are that you have installed the oauth2 gem using:
# gem install oauth2
# Then call this file using `ruby token.rb` and follow the instructions.
require 'rubygems'
require 'oauth2'
redirect_uri = 'https://localhost/oauth2callback'
puts "Make sure #{redirect_uri} is set as a 'Redirect URI' in the Google Conosle for the project you want to create a token for.\n"
puts "Enter Client ID from Google Console (and hit enter):\n"
client_id = gets.chomp.strip
puts "\nEnter Client secret from Google Console (and hit enter):\n"
client_secret = gets.chomp.strip
puts "\nEnter scope of the service you want e.g.: https://www.googleapis.com/auth/analytics.readonly\n"
scope = gets.chomp.strip
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"})
puts "\nPaste this URL into your browser\n\n"
puts auth_client_obj.auth_code.authorize_url(:scope => scope, :access_type => "offline", :redirect_uri => redirect_uri, :approval_prompt => 'force')
puts "\nAccept the authorization request. Google will then redirect you to localhost, 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 })
puts "\nToken is: #{access_token_obj.token}"
puts "Refresh token is: #{access_token_obj.refresh_token}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment