Storing Credentials for Offline Access in Datastore

7 views
Skip to first unread message

Jimmy Kane via StackOverflow

unread,
May 17, 2017, 8:43:06 AM5/17/17
to gcd-stac...@googlegroups.com

Example of usage:

Additional imports needed for the below code:

from google.appengine.api import users
from google.appengine.api import memcache
import pickle

Trigger flow example:

flow = OAuth2WebServerFlow(
            client_id=client_id,
            client_secret=client_secret,
            scope=scope,

            access_type = 'offline',
            approval_prompt='force',
            redirect_uri=self.request.relative_url('/admin/auth_return'))
# Get the url now
authorize_url = flow.step1_get_authorize_url()
# Save flow object in memcache for later retrieval on OAuth callback,
# and redirect this session to Google's OAuth 2.0 authorization site.
logging.info('saving flow for user ' + user.user_id())
memcache.set(user.user_id(), pickle.dumps(flow))
# redirect directly
self.redirect(authorize_url, abort=True)

Save

...

user = users.get_current_user()
logging.info('retrieving flow for user ' + user.user_id())
flow = pickle.loads(memcache.get(user.user_id()))

credentials = flow.step2_exchange(self.request.params)
StorageByKeyName(CredentialsModel, user.user_id(), 'credentials').locked_put(credentials)

Get

# here is how you can get them
credentials = StorageByKeyName(CredentialsModel, user.user_id(),'credentials').locked_get()
...


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/21461093/storing-credentials-for-offline-access-in-datastore/21462051#21462051
Reply all
Reply to author
Forward
0 new messages