def context_getsource(request): message_id = request.POST['message_id'] ctxIO = ContextIO(consumer_key=api_key, consumer_secret=api_secret) accntList = ctxIO.get_accounts(email=mailbox_to_query) logging.info('ctxIO Account: %r' % accntList) message = Account(ctxIO, { 'id': accntList[0].id }).get_message_source(message_id=message_id) _process_incoming_mail(message, '*****@*****.**', 'e/e/e/isworld@e') return True
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() # Figure out if there's an account corresponding to the current users's email address. # Normally, your app would maintain the Context.IO account id corresponding to each user # account but for the sake of keeping this example simple, we're checking every time and # setting a cookie with the account id every time an authentified user logs in this demo. ctxIO = ContextIO(consumer_key=settings.CONTEXTIO_OAUTH_KEY, consumer_secret=settings.CONTEXTIO_OAUTH_SECRET) accntList = ctxIO.get_accounts(email=current_email) if len(accntList) >= 1: # The mailbox is available under our Context.IO account # The following is to avoid implementing a user property storage for this app # (see comment above) self.response.headers.add_header( 'Set-Cookie', 'ctxioid=' + accntList[0].id + '; path=/;') m = md5.new() m.update(accntList[0].id) m.update(current_email) self.response.headers.add_header( 'Set-Cookie', 'ctxiohash=' + m.hexdigest() + '; path=/;') # 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: # 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: self.response.out.write( "Oops, forgot to login: required option for this script in app.yaml?" )
def context_buildindex(request, iolimit, iooffset): ctxIO = ContextIO(consumer_key=api_key, consumer_secret=api_secret) accntList = ctxIO.get_accounts(email=mailbox_to_query) logging.info('ctxIO Account: %r' % accntList) messages = Account(ctxIO, { 'id': accntList[0].id }).get_messages(limit=iolimit, offset=iooffset) txt = 'All done!' for message in messages: ctxmsg = models.Contextio(key_name=message.message_id) ctxmsg.message_date = message.date ctxmsg.put() # get messages with limit and offset return HttpResponse(txt, content_type='text/plain')
def get(self): current_email = users.get_current_user().email() if validateUser(self.request.cookies, current_email): emailAddr = self.request.get('email') ctxIO = ContextIO(consumer_key=settings.CONTEXTIO_OAUTH_KEY, consumer_secret=settings.CONTEXTIO_OAUTH_SECRET) messages = Contact( Account( ctxIO, {'id': self.request.cookies['ctxioid'].decode('utf-8')}), { 'email': emailAddr }).get_messages(limit=25) self.response.out.write(json.dumps(messages)) else: self.response.out.write('[]')
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('/')
def get(self): current_email = users.get_current_user().email() if validateUser(self.request.cookies, current_email): searchStr = self.request.get('str') ctxIO = ContextIO(consumer_key=settings.CONTEXTIO_OAUTH_KEY, consumer_secret=settings.CONTEXTIO_OAUTH_SECRET) contacts = Account( ctxIO, { 'id': self.request.cookies['ctxioid'].decode('utf-8') }).get_contacts(search=searchStr) respData = [] for i in contacts: respData.append({ 'name': i.name, 'email': i.email, 'thumbnail': i.thumbnail }) self.response.out.write(json.dumps(respData)) else: self.response.out.write('[]')