Skip to content

Instantly share code, notes, and snippets.

@holysugar
Created January 6, 2016 11:22
Show Gist options
  • Save holysugar/19a597668ac795761e31 to your computer and use it in GitHub Desktop.
Save holysugar/19a597668ac795761e31 to your computer and use it in GitHub Desktop.
fetch google api refresh token
#!/usr/bin/ruby
require 'jwt' # oops...
gem 'google-api-client', '0.8.6'
require 'google/api_client'
scope = [
'https://www.googleapis.com/auth/drive',
'https://spreadsheets.google.com/feeds/',
]
client_id = ARGV[0] || fail("set client_id")
print "input client_secret: "
client_secret = $stdin.readline.strip
puts
client = Google::APIClient.new(
:application_name => 'fetch-refresh-token.rb',
:application_version => '1.0.0'
)
auth = client.authorization
auth.client_id = client_id
auth.client_secret = client_secret
auth.scope = scope
auth.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
$stderr.print("\n1. Open this page:\n%s\n\n" % auth.authorization_uri)
system(%[open "#{auth.authorization_uri}"])
$stderr.print('2. Enter the authorization code shown in the page: ')
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
puts
puts "refresh_token: #{auth.refresh_token}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment