2014-07-22 145 views
2

我想從我的收件箱中有類似https://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py列出的示例代碼獲取消息的列表:的GMail API +服務帳戶

import httplib2 

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

SERVICE_ACCOUNT_ID="10*[email protected]" 
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"> 

我想知道我做錯了什麼?這似乎是我可以創建的服務帳戶的最簡單示例。

謝謝!

+1

你可以參考下面的問題 HTTP:/ /stackoverflow.com/questions/24779138/can-we-access-gmail-api-using-service-account/24795241#24795241 –

回答

4

您試圖訪問誰的郵箱?我相信這個錯誤告訴你,該服務帳戶(「10 * [email protected]」)沒有Gmail郵箱,在這種情況下,嘗試獲取其郵件是沒有用的,因爲它甚至不能接收郵件。

如果你想使用一個服務帳戶爲跨您管理網域用戶的訪問郵箱,看到其他評論者提到了答案:Can we access GMAIL API using Service Account?