GMAIL API不接受大多数查询(GAE Python)

时间:2015-04-20 15:30:38

标签: python google-app-engine gmail-api

我正在尝试使用Python上的Google App Engine应用程序获取过去3个月内发送的所有邮件。出于某种原因,虽然它不接受我输入的大多数查询。它返回一个简单字符串的结果,但如果我输入“after:2015/01/20”或“newer_than:3m”之类的内容,则会出现以下错误:

AttributeError: 'Resource' object has no attribute 'messages'

我不知道这可能来自哪里。我当前的请求代码是:

import webapp2, httplib2
from dateutil.relativedelta import *

from oauth2client.appengine import OAuth2Decorator
from apiclient import discovery, errors
from oauth2client import client
from google.appengine.api import memcache

http = httplib2.Http(memcache)
service = discovery.build("gmail", "v1", http=http)
decorator = OAuth2Decorator(client_id=settings.CLIENT_ID,
                        client_secret=settings.CLIENT_SECRET,
                        scope=settings.SCOPE)

class retrieveMessages(webapp2.RequestHandler):

  @decorator.oauth_required
  def get(self):
    try:
        user = 'someone@gmail.com'
        after = (datetime.datetime.now()+relativedelta(months=-3)).strftime("%Y/%m/%d")
        query = 'after:'+after
        http = decorator.http()
        response = service.users().messages().list(userId=user, labelIds='SENT', q=query, maxResults=1000).execute(http=http)
        messages = []
        if 'messages' in response:
            messages.extend(response['messages'])

        while 'nextPageToken' in response:
          page_token = response['nextPageToken']
          response = service.users().messages().list(userId=user, labelIds='SENT', q=query, pageToken=page_token).executehttp=http(http=http)
          messages.extend(response['messages'])

        return messages
    except errors.HttpError, error:
        print 'An error occurred: %s' % error
        if error.resp.status == 401:
          # Credentials have been revoked.
          # TODO: Redirect the user to the authorization URL.
          raise NotImplementedError()

1 个答案:

答案 0 :(得分:2)

不应该在while循环中使用此代码段:

response = service.messages()...

response = service.users().messages()...