Created
March 5, 2014 09:04
-
-
Save edds/9363713 to your computer and use it in GitHub Desktop.
Generate a Google api refresh token to authenticate API requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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