Skip to content

Instantly share code, notes, and snippets.

@martinhbramwell
Last active December 28, 2015 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinhbramwell/7553524 to your computer and use it in GitHub Desktop.
Save martinhbramwell/7553524 to your computer and use it in GitHub Desktop.
Simple command line script to prepare a full set of Google API's OAuth2 credentials as Python variables.
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
'''
This script will attempt to open your web browser,
perform OAuth 2 authentication and print out your full set
of OAuth credentials in the form of Python variable declarations.
It depends on Google's Python library: oauth2client.
To install that dependency from PyPI:
$ pip install oauth2client
Then run this script:
$ python get_google_oauth2_creds.py
This is a combination of snippets from:
https://developers.google.com/api-client-library/python/guide/aaa_oauth
'''
import sys, argparse
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run_flow
from oauth2client.file import Storage
from oauth2client.util import POSITIONAL_WARNING, POSITIONAL_EXCEPTION, POSITIONAL_IGNORE
class NameSpace(object):
def __init__(self, adict):
self.__dict__.update(adict)
def main(id, secret):
flow = OAuth2WebServerFlow(client_id=id,
client_secret=secret,
scope='https://spreadsheets.google.com/feeds https://docs.google.com/feeds',
redirect_uri='http://example.com/auth_return')
storage = Storage('creds.data')
flags = NameSpace({'noauth_local_webserver': False, 'auth_host_port' : [8080, 8090], 'auth_host_name' : 'localhost', 'logging_level' : POSITIONAL_WARNING})
credentials = run_flow(flow, storage, flags)
print ""
print ""
print " Create an empty text file called qiktest.py"
print " Paste the following lines into it."
print " Execute with:"
print " $ python qiktest.py"
print ""
print ""
print "# - - - - - - - - - - - - - - - - - - - - - - - - - - -"
print "# -*- coding: utf-8 -*-"
print "import gspread"
print "#"
print "refresh_token = '{}'".format(credentials.refresh_token)
print "client_secret = '{}'".format(secret)
print "client_id = '{}'".format(id)
print "#"
print "cred_type = 'oauth'"
print "key_ring = {}"
print "key_ring['grant_type'] = 'refresh_token'"
print "key_ring['refresh_token'] = refresh_token"
print "key_ring['client_secret'] = client_secret"
print "key_ring['client_id'] = client_id"
print "#"
print "access_token = '{}'".format(credentials.access_token)
print "gc = gspread.authorize(access_token, key_ring)"
print "#"
print "wkbk = gc.open_by_url('{}')".format(spreadsheet_url)
print "cnt = 1"
print "print 'Found sheets:'"
print "for sheet in wkbk.worksheets():"
print " print ' - Sheet #{}: Id = {} Title = {}'.format(cnt, sheet.id, sheet.title)"
print " cnt += 1"
print "#"
print "# - - - - - - - - - - - - - - - - - - - - - - - - - - -"
print ""
print ""
if __name__ == '__main__':
print 'You will be asked for a Google "Client ID", a "Client Secret" and the URL of the spreadsheet you want to access.'
print 'To get the ID and Secret, follow the procedure here :'
print ''
print ' https://github.com/FleetingClouds/gspread/wiki/How-to-get-OAuth-access-token-in-console%3F.'
print ''
client_id = raw_input('Paste your Google "Client ID" : ')
client_secret = raw_input('Paste your Google "Client Secret" : ')
spreadsheet_url = raw_input('Paste the full URL, including the "http://" etc, etc, of the Google spreadsheet you want to use : ')
main(client_id, client_secret)
@SalvaJ
Copy link

SalvaJ commented Mar 18, 2014

If I change print by print() and raw_input() by input, it'll works in Python3?

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