Gmail API python 实施

Gmail API python implementation

我正在实施一个可以阅读电子邮件的机器人,并且我正在关注 Gmail API。我可以阅读所有标签并将其存储在列表中。我无法阅读标签内的消息

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())

我得到一个错误 -

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

官方 api 文档实现 get label 在 java 中。 有人可以告诉我我做错了什么吗?

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'

我认为这是你应该做的:

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

来自官方文档: https://developers.google.com/gmail/api/quickstart/python

进一步阅读后,这似乎是一个 2 阶段过程。

首先您需要通过查询获取消息列表:

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

然后通过消息 ID 抓取消息:

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

此处的错误是您将 'get' 方法用于标签而不是消息。此 get 方法用于查找有关标签的信息,例如该标签内的未读消息数。

您可以在下面的行中看到您要求的是 .labels

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

你是对的,这个例子只出现在 Java 中。如果您希望 get(用于标签)方法在 python 中工作,这里是要使用的代码示例。

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