def get(self): """Handle code exchange.""" code = self.request.get('code') if not code: # TODO: Display error. return None oauth_flow = self.create_oauth_flow() # Perform the exchange of the code. If there is a failure with exchanging # the code, return None. try: creds = oauth_flow.step2_exchange(code) except FlowExchangeError: # TODO: Display error. return None users_service = util.create_service('oauth2', 'v2', creds) # TODO: Check for errors. user = users_service.userinfo().get().execute() userid = user.get('id') username = user.get('name') # Store the credentials in the data store using the userid as the key. # TODO: Hash the userid the same way the userToken is. """StorageByKeyName(Credentials, userid, 'credentials').put(creds)""" entity = Credentials(name = username, credentials = creds, key_name = userid) entity.put() logging.info('Successfully stored credentials for user: %s', entity) util.store_userid(self, userid) self._perform_post_auth_tasks(userid, creds) self.redirect('/')