Python / Flask google api integration -


i'm new python in flask... have developed python file sets oauth2 authentication googele , fetching list of messages gmail api. here code

import json import flask import httplib2 import base64 import email  apiclient import discovery, errors oauth2client import client   app = flask.flask(__name__)   @app.route('/') def index():     if 'credentials' not in flask.session:         return flask.redirect(flask.url_for('oauth2callback'))     credentials = client.oauth2credentials.from_json(flask.session['credentials'])     if credentials.access_token_expired:         return flask.redirect(flask.url_for('oauth2callback'))     else:         http_auth = credentials.authorize(httplib2.http())         gmail_service = discovery.build('gmail', 'v1', http_auth)         threads = gmail_service.users().threads().list(userid='me').execute()         return json.dumps(threads)   @app.route('/oauth2callback') def oauth2callback():     flow = client.flow_from_clientsecrets(         'client_secrets.json',         scope='https://mail.google.com/',         redirect_uri=flask.url_for('oauth2callback', _external=true)     )     if 'code' not in flask.request.args:         auth_uri = flow.step1_get_authorize_url()         return flask.redirect(auth_uri)     else:         auth_code = flask.request.args.get('code')         credentials = flow.step2_exchange(auth_code)         flask.session['credentials'] = credentials.to_json()         return flask.redirect(flask.url_for('index'))  @app.route('/getmail') def getmail():     if 'credentials' not in flask.session:         return flask.redirect(flask.url_for('oauth2callback'))     credentials = client.oauth2credentials.from_json(flask.session['credentials'])     if credentials.access_token_expired:         return flask.redirect(flask.url_for('oauth2callback'))     else:         http_auth = credentials.authorize(httplib2.http())         gmail_service = discovery.build('gmail', 'v1', http_auth)         query = 'is:inbox'         """list messages of user's mailbox matching query.          args:         service: authorized gmail api service instance.         user_id: user's email address. special value "me"         can used indicate authenticated user.         query: string used filter messages returned.         eg.- 'from:user@some_domain.com' messages particular sender.          returns:         list of messages match criteria of query. note         returned list contains message ids, must use         appropriate id details of message.         """         try:             response = gmail_service.users().messages().list(userid='me', q=query).execute()             messages = []             if 'messages' in response:                 print 'test %s' % response                 messages.extend(response['messages'])             while 'nextpagetoken' in response:                 page_token = response['nextpagetoken']                 response = gmail_service.users().messages().list(userid='me', q=query, pagetoken=page_token).execute()                 messages.extend(response['messages'])              return messages         except errors.httperror, error:             print 'an error occurred: %s' % error  if __name__ == '__main__':     import uuid     app.secret_key = str(uuid.uuid4())     app.debug = true     app.run() 

authentication works fine , when go /getmail url getting error typeerror: 'list' object not callable

any thoughts doing wrong?

i changed return object in flask return messages piece of code.

first imported from flask.json import jsonify

try:     response = gmail_service.users().messages().list(userid='me', q=query).execute()     messages = []     if 'messages' in response:         print 'test %s' % response         messages.extend(response['messages'])     while 'nextpagetoken' in response:         page_token = response['nextpagetoken']         response = gmail_service.users().messages().list(userid='me', q=query, pagetoken=page_token).execute()         messages.extend(response['messages'])      return jsonify({'data': messages}) # changed here except errors.httperror, error:     print 'an error occurred: %s' % error 

all credit goes @doru


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -