Python 如何正确监视gmail推送通知

Python 如何正确监视gmail推送通知,python,push-notification,google-api,gmail-api,Python,Push Notification,Google Api,Gmail Api,我不知道如何正确地监视gmail推送通知(而且我对网络编码一无所知) 谷歌提供本教程页面: 到目前为止我所做的: 先决条件:完成 创建一个主题:完成 创建订阅:完成 授予您的主题的发布权限:完成 watch()方法工作并给出以下结果:{'historyId':'714707','expiration':'1618824687477'} 到目前为止一切正常,但我的知识仅限于此。在我看来,我必须在watch方法上实现某种无限while循环,以检查historyId更改。但是如果是这样,为什么在w

我不知道如何正确地监视gmail推送通知(而且我对网络编码一无所知)

谷歌提供本教程页面:

到目前为止我所做的:

  • 先决条件:完成
  • 创建一个主题:完成
  • 创建订阅:完成
  • 授予您的主题的发布权限:完成
  • watch()方法工作并给出以下结果:
    {'historyId':'714707','expiration':'1618824687477'}
到目前为止一切正常,但我的知识仅限于此。在我看来,我必须在watch方法上实现某种无限while循环,以检查
historyId
更改。但是如果是这样,为什么在
watch()
方法上会有
stop()
方法,为什么在watch结果上会有
expiration
字段? 从那里我该怎么办

以下是我迄今为止的实施情况:

from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials


SCOPES = ['https://mail.google.com/']
MY_TOPIC_NAME = 'my/toppic/name'

creds = None
if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
else:
        flow = InstalledAppFlow.from_client_secrets_file('./my_creds.json', SCOPES)
        creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
        token.write(creds.to_json())



gmail = build('gmail', 'v1', credentials=creds)
request = {'labelIds': ['INBOX'],'topicName': MY_TOPIC_NAME}
gmail.users().watch(userId='me', body=request).execute()
我认为对推送通知的概念存在一些误解
  • 设置监视请求后,当出现邮箱更新时,应用程序将自动接收推送通知
  • 例如,当您收到一封新电子邮件时,会发生邮箱更新
  • 简而言之,
    historyId
    反映了您邮箱在特定时刻的状态(例如,当您设置监视请求时)
  • 此值仅供参考,因为除非您仅收到与特定
    historyId
  • 您不需要无休止的循环来接收通知,您只需要这样做
  • 根据您的实现,您可能需要一些HTTP库来处理POST请求——只要Gmail API向您的服务器发送通知

谢谢,我会在有时间的时候对此进行深入研究,并将我的整个解决方案发布在这里