GMAIL API 不接受大多数查询 (GAE Python)
GMAIL API doesn't accept most queries (GAE Python)
我正在尝试使用 Python 上的 Google App Engine 应用程序获取过去 3 个月内所有已发送的消息。出于某种原因,它不接受我输入的大部分查询。它 returns 结果是一个简单的字符串,但如果我输入类似 "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()
这段代码不应该在 while 循环中:
response = service.messages()...
成为
response = service.users().messages()...
我正在尝试使用 Python 上的 Google App Engine 应用程序获取过去 3 个月内所有已发送的消息。出于某种原因,它不接受我输入的大部分查询。它 returns 结果是一个简单的字符串,但如果我输入类似 "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()
这段代码不应该在 while 循环中:
response = service.messages()...
成为
response = service.users().messages()...