2

我正在尝试从我的收件箱中获取邮件列表,其代码类似于https://code.google.com/p/google-api-python-client/source/browse/samples/service_account/中列出的示例任务.py

import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import SignedJwtAssertionCredentials

SERVICE_ACCOUNT_ID="10*0@developer.gserviceaccount.com"
KEY_FILE="/Users/*/gmail-sessionid-privatekey.p12"
KEY_SECRET="notasecret"

f = open("/Users/*/gmail-sessionid-privatekey.p12")
key = f.read()
f.close()

credentials = SignedJwtAssertionCredentials(
    SERVICE_ACCOUNT_ID, key, scope="https://www.googleapis.com/auth/gmail.readonly")
http = httplib2.Http()
http = credentials.authorize(http)
service = build("gmail", "v1", http=http)

response = service.users().messages().list(userId='me', q='').execute(http=http)
messages = response['messages']
print messages

我得到的回应是:

Traceback (most recent call last):
  File "service.py", line 21, in <module>
    response = service.users().messages().list(userId='me', q='').execute(http=http)
  File "/Library/Python/2.7/site-packages/oauth2client/util.py", line 132, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/apiclient/http.py", line 723, in execute
    raise HttpError(resp, content, uri=self.uri)
apiclient.errors.HttpError: <HttpError 500 when requesting https://www.googleapis.com/gmail/v1/users/me/messages?q=&alt=json returned "Backend Error">

我试图找出我做错了什么?这似乎是我可以创建的最简单的服务帐户示例。

谢谢!

4

1 回答 1

4

您要访问谁的邮箱?我相信这个错误告诉你服务帐户(“10 * 0@developer.gserviceaccount.com”)没有Gmail邮箱,在这种情况下尝试获取它的消息是没有用的,因为它甚至无法接收邮件。

如果您想使用服务帐户访问您管理的域中用户的邮箱,请参阅其他评论者提到的答案:我们可以使用服务帐户访问 GMAIL API 吗?

于 2014-07-22T17:04:07.503 回答