此代码使用 gmail api 在 gmail 中为我提供了特定用户的第一条消息。
如何使用 gmail-api 在我的 gmail 中获取两个特定用户的第一条消息 我可以在这里使用 if else 语句吗?
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import time
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
def main():
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
# Call the Gmail API to fetch INBOX
#results = service.users().messages().list(userId='me',maxResults=10,labelIds = ['INBOX']).execute()
results = service.users().messages().list(userId='me', q='googlecommunityteam-noreply@google.com',labelIds = ['INBOX']).execute()
messages = results.get('messages', [])
if not messages:
print ("\nGmail: \n No gmail mails found.\n")
else:
print ("Gmail:\n")
msg = service.users().messages().get(userId='me', id=messages[0]['id']).execute()
print(msg['snippet'])
print(msg['internalDate'])
if __name__ == '__main__':
main()
HUWWW
相关分类