Ejemplo n.º 1
0
 def get(self, user_email):
   flow = createFlow(self, user_email)
   credentials = StorageByKeyName(CredentialsModel, user_email, 'credentials').get()
   force = self.request.get('force')
   if force and force == 'true':
       self.redirect(flow.step1_get_authorize_url()) 
       return
   
   if credentials:
     user = User.get_by_key_name(user_email)
     if not user or not user.is_oauth_complete:
       ctxIO = ContextIO(consumer_key=settings.CONTEXTIO_OAUTH_KEY, 
                         consumer_secret=settings.CONTEXTIO_OAUTH_SECRET)
       current_account = ctxIO.post_account(email=user_email)
       user = User.get_or_insert(key_name = user_email, 
                                 user_ctx_id=current_account.id,
                                 email=user_email)
       refresh_token = credentials.refresh_token
       try:
         if not refresh_token:
           raise Exception('no refresh token')
         current_account.post_source(email=user_email,
                                            username=user_email,
                                            server='imap.gmail.com',
                                            provider_refresh_token=refresh_token,
                                            provider_consumer_key=settings.APPENGINE_CONSUMER_KEY)
       except Exception as e:
         logging.error(str(e))
         self.redirect(flow.step1_get_authorize_url())
       user.is_oauth_complete = True
       user.put()
     self.response.out.write(r"""<html><head><script type="text/javascript">window.close();</script></head><body><div id="sbi_camera_button" class="sbi_search" style="left: 0px; top: 0px; position: absolute; width: 29px; height: 27px; border: none; margin: 0px; padding: 0px; z-index: 2147483647; display: none;"></div></body></html>""")
   else:
     logging.info('redirect')
     self.redirect(flow.step1_get_authorize_url()) 
Ejemplo n.º 2
0
    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.
        #
        # 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/imapoauth
        ctxIO = ContextIO(consumer_key=settings.CONTEXTIO_OAUTH_KEY,
                          consumer_secret=settings.CONTEXTIO_OAUTH_SECRET)
        newAccount = ctxIO.post_account(email=current_user.email(),
                                        first_name=current_user.nickname())
        newSource = newAccount.post_source(
            email=current_user.email(),
            username=current_user.email(),
            server='imap.gmail.com',
            provider_token=gdocs.auth_token.token,
            provider_token_secret=gdocs.auth_token.token_secret,
            provider_consumer_key=settings.APPENGINE_CONSUMER_KEY)

        # 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('/')
Ejemplo n.º 3
0
	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)
		
		import logging
		logging.info('auth_token %s request_token %s'%(str(gdocs.auth_token),str(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. 
		#
		# 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/imapoauth
		ctxIO = ContextIO(consumer_key=settings.CONTEXTIO_OAUTH_KEY,consumer_secret=settings.CONTEXTIO_OAUTH_SECRET)
		newAccount = ctxIO.post_account(email=current_user.email(),first_name=current_user.nickname())
		newSource = newAccount.post_source(email=current_user.email(), 
						username=current_user.email(),
						server='imap.gmail.com',
						provider_token=gdocs.auth_token.token,
						provider_token_secret=gdocs.auth_token.token_secret,
						provider_consumer_key=settings.APPENGINE_CONSUMER_KEY)
						
				
		# 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('/')