2

I'm trying to get a list of messages from my Inbox with the code similar to the example listed in 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

The response I get is:

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">

I am trying to find out what I am doing wrong? This seems to be the simplest example of service accounts I can create.

Thanks!

MeLight
  • 5,454
  • 4
  • 43
  • 67
Debnath Sinha
  • 1,087
  • 1
  • 12
  • 25
  • 1
    you can refer to the below question http://stackoverflow.com/questions/24779138/can-we-access-gmail-api-using-service-account/24795241#24795241 – Haseena Parkar Jul 22 '14 at 13:05

1 Answers1

4

Whose mailbox are you trying to access? I believe this error is telling you that the service account ("10*0@developer.gserviceaccount.com") does not have a Gmail mailbox, in which case there is no use trying to fetch its messages because it can't even receive mail.

If you want to use a service account to access mailboxes of users across a domain that you administer, see the answer that the other commenter referred to: Can we access GMAIL API using Service Account?

Community
  • 1
  • 1