[英]Is there a way to get the first emails sent by two users to my gmail by using Gmail API? My code gets for one users first email to me
此代碼使我使用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()
Gmail搜索中的Q參數與Gmail網站中的搜索完全一樣,因此,如果您使用gmail並單擊搜索框中右側的箭頭,則該參數將與Gmail中的參數完全相同。 向上將彈出搜索構建器
您可以在其中玩轉,並調出您想要的任何類型的搜索
from:(Test@gmail.com) to:(me@gmail.com)
然后,您可以將其直接放入請求中的q中
results = service.users().messages().list(userId='me', q='from:(Test@gmail.com) to:(me@gmail.com)',labelIds = ['INBOX']).execute()
現在所有這些我都在玩,如果您輸入兩個電子郵件地址,我將無法使它返回兩個不同用戶的電子郵件,它只能返回他們在現場的任何地方的電子郵件。 因此,我認為您將能夠找到兩個不同用戶發送給您的第一封電子郵件的唯一方法是
results = service.users().messages().list(userId='me', q='from:(user1@gmail.com) to:(me@gmail.com)',labelIds = ['INBOX']).execute()
results = service.users().messages().list(userId='me', q='from:(user2@gmail.com) to:(me@gmail.com)',labelIds = ['INBOX']).execute()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.