Skip to content

Instantly share code, notes, and snippets.

@zopieux
Created December 28, 2014 14:48
Show Gist options
  • Save zopieux/22e18ecda720f0c67e01 to your computer and use it in GitHub Desktop.
Save zopieux/22e18ecda720f0c67e01 to your computer and use it in GitHub Desktop.
Google API OAuth2 persistent token
# Example with Contacts API
import atom.http_core
import gdata.contacts.client
import gdata.gauth
auth_kwargs = {
'client_id': 'clielnt-id-xxx.apps.googleusercontent.com', # CHANGEME (see API auth console)
'client_secret': 'client-secret-xxx', # CHANGEME (see API auth console)
'scope': 'https://www.google.com/m8/feeds', # CHANGEME (see API reference)
'user_agent': 'Mozila/5.0',
}
if __name__ == '__main__':
client = gdata.contacts.client.ContactsClient(source='ContactsClient')
try:
with open('gdata.auth.contacts') as f:
token = gdata.gauth.token_from_blob(f.read())
token = gdata.gauth.OAuth2Token(refresh_token=token.refresh_token, **auth_kwargs)
except IOError:
token = gdata.gauth.OAuth2Token(**auth_kwargs)
print(token.generate_authorize_url(redirect_uri='http://localhost'))
url = raw_input("Paste redirect URL: ")
url = atom.http_core.ParseUri(url)
token.get_access_token(url.query)
with open('gdata.auth.contacts', 'w') as f:
f.write(gdata.gauth.token_to_blob(token))
token.authorize(client)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment