Using existing access tokens with oauth2client

Was recently pulling my hair for a hour to figure out how I can use a existing access token I might have from somewhere else in my application with the Google API Client.

And after much searching, happened on AccessTokenCredentials which is explained shortly at https://developers.google.com/api-client-library/python/guide/aaa_oauth#AccessTokenCredentials. With the very vague description of:

The oauth2client.client.AccessTokenCredentials class is used when you have already obtained an access token by some other means. You can create this object directly without using a Flow object.

Awesome that's what I need. But HOW ?

After looking at the docs and source code I managed to get it working by using the following sample:

at_credentials = AccessTokenCredentials(access_token_str, 'some-useragent/1.0')  
http = httplib2.Http()  
http = at_credentials.authorize(http)

Hope this saves someone a bit of trouble. No go and call some Google API's !

Also rather important if you are using the library like this on App Engine be sure to read the section on ThreadSafe for the library ... - https://developers.google.com/api-client-library/python/guide/thread_safety


Posted on 2014/12/31 by Johann du Toit

comments powered by Disqus