Gmail pub/sub API for Python

61 Views Asked by At

I need to monitor a Gmail INBOX for new emails and when that happens, a push notification should go a webhook hosted in flask app, so I can take action based on the new email.

How can this be achieved using Pub/Sub? If it is already addressed somewhere else, please share the reference.

I did the following

  1. Created and deployed the flask app to GAE
  2. Obtained the credentials JSON by enabling and configuring Gmail API
  3. Tested the connectivity and is working fine.
  4. Created a topic and a subscriber

What I am not sure is how to link this to the webhook URL and actively listen for any new emails.

1

There are 1 best solutions below

0
NoCommandLine On
  1. Follow Google's documentation

  2. Note that it says

    You must re-call watch at least every 7 days or else you will stop receiving updates for the user. We recommend calling watch once per day.

    I suggesting using Cloud Scheduler to invoke an App Engine handler that calls watch daily or every 7 days

  3. Since you're trying to read gmails, you'll need authorization via OAuth 2.0 credentials. If your App is set to external users and isn't published, your token will expire every 7 days (see doc)