GodSaveTheDucks
GodSaveTheDucks

Reputation: 756

Gmail API python implementation

I'm implementing a bot where I can read emails and I'm following the Gmail API. I can read all the labels and I have stored it in the list. I am not able to read the messages inside the label

credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
results = service.users().labels().get('me',"INBOX").execute()
print (results.getName())

and I get an error -

results = service.users().labels().get('me',"INBOX").execute()
TypeError: method() takes exactly 1 argument (3 given)

The official api docs implementation get label is in java. Can someone please tell me what I'm doing wrong?

SCOPES = 'https://www.googleapis.com/auth/gmail.readonly','https://mail.google.com/','https://www.googleapis.com/auth/gmail.modify','https://www.googleapis.com/auth/gmail.labels'

Upvotes: 3

Views: 1603

Answers (2)

Dan Heaford
Dan Heaford

Reputation: 451

The mistake here is that you are using the 'get' method for labels rather than for messages. This get method is used to find out information about a label, such as the number of unread messages inside that label.

You can see on the line below you are asking for the .labels

results = service.users().labels().get('me',"INBOX").execute()

You are correct that this example is only shown in Java. If you want the get (for labels) method to work in python here is an example of the code to use.

results = service.users().labels().get(userId='me',id='Label_15').execute()

Upvotes: 0

Max Uppenkamp
Max Uppenkamp

Reputation: 974

I think this is what you are supposed to do:

 results = service.users().labels().list(userId='me').execute()

From the official documentation: https://developers.google.com/gmail/api/quickstart/python

Upon further reading, this seems to be a 2 stage process.

First you need to grab the list of messages with a query:

response = service.users().messages().list(userId=user_id, q=query,
                                     pageToken=page_token).execute()

Then you grab the message by its ID:

message = service.users().messages().get(userId=user_id, id=msg_id).execute()

Upvotes: 2

Related Questions