Hi

Google mail api subscription steps

 

To create a subscriptionId for Gmail Push Notifications using Google Pub/Sub, follow these steps:


 Prerequisites:

  1. A Google Cloud Project

  2. Gmail API and Pub/Sub API enabled

  3. A Pub/Sub topic created in the same project

  4. A service account with Pub/Sub Publisher and Gmail API permissions

  5. Domain-wide delegation enabled (if accessing multiple users)


🔧 Step-by-Step: Creating a subscriptionId

🔹 Step 1: Create a Pub/Sub Topic

Go to the Google Cloud Console:

  • Navigate to Pub/Sub > Topics

  • Click "Create Topic"

  • Name it: projects/YOUR_PROJECT_ID/topics/YOUR_TOPIC_NAME

🔹 Step 2: Create a Subscription to That Topic

Once your topic is created:

  1. Go to Pub/Sub > Subscriptions

  2. Click "Create Subscription"

  3. Set:

    • Subscription ID: e.g., gmail-updates-sub

    • Topic: Select your topic

    • Delivery Type: Usually Push

    • Endpoint URL: Must be an HTTPS endpoint that can receive Gmail push events (e.g., a webhook)

🔹 Step 3: Grant Gmail Publish Rights

Gmail publishes to Pub/Sub using a special service account:

  • Email: gmail-api-push@system.gserviceaccount.com

So, go to IAM > Permissions for your topic and:

  • Add a new member: gmail-api-push@system.gserviceaccount.com

  • Role: Pub/Sub Publisher

🔹 Step 4: Use subscriptionId in Your App (optional)

While Gmail uses the topic name, you can use the subscription on your server to receive and process messages.

🧪 Testing

You can publish a test message in the Pub/Sub topic, and verify your endpoint (or logging) receives the notification.


🧠 Notes

  • Gmail doesn't use the subscription ID directly. It publishes to the topic, and you subscribe to the topic to receive notifications.

  • Your Windows Service should either:

    • Periodically check using HistoryId polling (simpler)

    • OR have a separate HTTP endpoint (like a web server or Azure Function) to receive real-time push

Previous
Next Post »