Skip to content

Instantly share code, notes, and snippets.

@burnash
Last active August 11, 2021 21:16
Show Gist options
  • Save burnash/6771295 to your computer and use it in GitHub Desktop.
Save burnash/6771295 to your computer and use it in GitHub Desktop.
Simple command line script to fetch a Google API's access token.
'''
This script will attempt to open your webbrowser,
perform OAuth 2 authentication and print your access token.
It depends on two libraries: oauth2client and gflags.
To install dependencies from PyPI:
$ pip install python-gflags oauth2client
Then run this script:
$ python get_oauth2_token.py
This is a combination of snippets from:
https://developers.google.com/api-client-library/python/guide/aaa_oauth
'''
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from oauth2client.file import Storage
CLIENT_ID = '<Client ID from Google API Console>'
CLIENT_SECRET = '<Client secret from Google API Console>'
flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope='https://spreadsheets.google.com/feeds https://docs.google.com/feeds',
redirect_uri='http://example.com/auth_return')
storage = Storage('creds.data')
credentials = run(flow, storage)
print "access_token: %s" % credentials.access_token
@Ichizaemon
Copy link

Ichizaemon commented Oct 19, 2016

Hi,

I'm trying to run your code but I get this error :

ImportError: cannot import name run

Do you know how can I fix it ?

@philsch
Copy link

philsch commented Oct 29, 2016

Try to replace run with run_flow. Seems like the function name has changed in oauth2client.tools.

@hehez
Copy link

hehez commented Nov 16, 2016

Agree with @philsch, the run is deprecated, run_flow instead of it.

@mbdevpl
Copy link

mbdevpl commented Sep 3, 2019

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