def get(self): """When the user grants access, they are redirected back to this handler where their authorized request token is exchanged for a long-lived access token.""" current_user = users.get_current_user() # Remember the token that we stashed? Let's get it back from # datastore now and adds information to allow it to become an # access token. request_token_key = 'request_token_%s' % current_user.user_id() request_token = gdata.gauth.ae_load(request_token_key) gdata.gauth.authorize_request_token(request_token, self.request.uri) # We can now upgrade our authorized token to a long-lived # access token by associating it with gdocs client, and # calling the get_access_token method. gdocs.auth_token = gdocs.get_access_token(request_token) # Now that we have a long-lived access token giving us access to # the user's mailbox, the last step is to add the mailbox in our # Context.IO account. This is done using the /imap/addaccount API # call and by passing the access token and the consumer key used # to obtain that token. # # For this to work, the Google Consumer key and secret used to obtain # access tokens must be configured in your Context.IO account. See: # https://console.context.io/#settings contextIO = IMAPAdmin(api_key=settings.CONTEXTIO_OAUTH_KEY, api_secret=settings.CONTEXTIO_OAUTH_SECRET, api_url=settings.CONTEXTIO_API_URL) info = contextIO.add_account(current_user.email(), current_user.email(), firstname=current_user.nickname(), server='imap.gmail.com', usessl=True, port=993, oauthtoken=gdocs.auth_token.token, oauthtokensecret=gdocs.auth_token.token_secret, oauthconsumername=settings.APPENGINE_CONSUMER_KEY ).get_data() # OPTIONAL: # If we want to keep the access token in our application persistent # storage, uncomment the 2 lines below would be used. Note that once # the mailbox has been added to our Context.IO, this is not required. #access_token_key = 'access_token_%s' % current_user.user_id() #gdata.gauth.ae_save(request_token, access_token_key) self.redirect('/')