def get(self): if users.get_current_user(): # Get the email address of the user and see if that mailbox is available # in our Context.IO account by using the /imap/accountinfo.json API call current_user = users.get_current_user() current_email = current_user.email() imapAdmin = IMAPAdmin(api_key=settings.CONTEXTIO_OAUTH_KEY, api_secret=settings.CONTEXTIO_OAUTH_SECRET, api_url=settings.CONTEXTIO_API_URL) user_info = imapAdmin.account_info(current_email).get_data() if user_info['id'] is None: # This mailbox isn't available under our Context.IO account, show # users the page prompting them to connect their Gmail account # through OAuth. See imapoauth.py for steps on obtaining Access # Token and adding the mailbox to our Context.IO account. template_values = { 'connect_link': '/imapoauth_step1' } path = os.path.join(os.path.dirname(__file__), 'templates', 'connect.html') self.response.out.write(template.render(path, template_values)) else: # The mailbox is available under our Context.IO account, show the # application main UI. This UI will get data from the mailbox through # XMLHttpRequest calls handled by handlers.py url = users.create_logout_url(self.request.uri) template_values = { 'url': url, 'url_linktext': 'sign out', } path = os.path.join(os.path.dirname(__file__), 'templates', 'app.html') self.response.out.write(template.render(path, template_values)) else: self.response.out.write("Oops, forgot to out login: required option for this script in app.yaml?")
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('/')