GMail API +服务帐户

时间:2014-07-22 12:14:09

标签: google-apps-marketplace gmail-api service-accounts

我正在尝试从我的收件箱中获取一个邮件列表,其代码类似于https://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.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

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

我得到的回应是:

我想知道我做错了什么?这似乎是我可以创建的最简单的服务帐户示例。

谢谢!

1 个答案:

答案 0 :(得分:4)

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

如果您想使用服务帐户访问您管理的域中的用户的邮箱,请参阅另一位评论者提到的答案:Can we access GMAIL API using Service Account?