# Gmail

## Credentials

References:

[https://developers.google.com/identity/protocols/oauth2](https://developers.google.com/identity/protocols/oauth2)

Procedure:

1. Create a OAuth Client ID

   <div class="docs-screenshot-compact">

   ![Create oauth client id](./assets/create-oauth-client-id.png)

   </div>

2. Enable the Gmail API in Google Cloud Platform
   - Search for "Gmail API" in the Google Cloud search bar
   - Click on Enable button

During the creation of the client, you must add your **instance's callback URL** to authorized redirect URLs field.

```jsx
https://credentials-INSTANCE-NAME/oauth2/callback
```

   <div class="docs-screenshot-compact">

   ![Click enable button](./assets/click-enable-button-2.png)

   </div>

   <div class="docs-screenshot-compact">

   ![Click enable button](./assets/click-enable-button.png)

   </div>

1. In Mindflow, you can create a credential in the Vault (_see screenshot_). To avoid having to refresh the token every 15 minutes, add **`?access_type=offline&prompt=consent`** in the Authorization Url field.

   <div class="docs-screenshot-compact">

   ![Mindflow you can create credential vault](./assets/mindflow-you-can-create-credential-vault.png)

   </div>

2. After selecting the Gmail service in the **Google portofolio**>Workspace you can now send mail with the operation: **Sends the specified message to the recipients in the To, Cc, and Bcc headers**

   <div class="docs-screenshot-compact">

   ![After selecting gmail service google portofolio](./assets/after-selecting-gmail-service-google-portofolio.png)

   </div>

   ```text
   From: <SENDER ADDRESS>
   Subject: "subject"
   To: <RECIPIENT ADDRESS>
   Content-Type: text/html; charset='UTF-8'

   Message Content !
   ```

## Webhooks

References:

- [https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications](https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications)

Procedure:

1. Enable Domain-wide Delegation
   - Go to Google Admin in the `Security` tab
   - Click on the `Access and Data Control` > `API controls`
   - Scroll down and click on manage delegation at the domain level
   - Click on `Add New`, enter your client id and click on`Authorize`
2. Create a topic and subscription
   - Go to [https://console.cloud.google.com/cloudpubsub/topic/](https://console.cloud.google.com/cloudpubsub/topic/l)
   - Click on `Create Topic` Create Topic button
   - Screenshots

   <div class="docs-screenshot-compact">

   ![Click create topic create topic button](./assets/click-create-topic-create-topic-button.png)

   </div>

   <div class="docs-screenshot-compact">

   ![topic 2.png](./assets/topic-2.png)

   </div>

3. Configure subscription to push notifications to Mindflow
   - Go to [https://console.cloud.google.com/cloudpubsub/subscription/](https://console.cloud.google.com/cloudpubsub/subscription/c)
   - Click on `Create Subscription` button
   - Get the address for webhooks in[Run playbook through webhook](https://app.notion.com/p/Run-playbook-through-webhook-e098912ed7d947daa00b8b0c0f535405?pvs=21)
   - Copy this address including the api key into subscription push address
     Note: we do not support JWT authorization yet
   - Screenshots

   <div class="docs-screenshot-compact">

   ![Copy address including api key into](./assets/copy-address-including-api-key-into.png)

   </div>

   <div class="docs-screenshot-compact">

   ![sub 2.png](./assets/sub-2.png)

   </div>

4. Grant publish rights on your topic
   - Go to [https://console.cloud.google.com/cloudpubsub/topic/](https://console.cloud.google.com/cloudpubsub/topic/l)
   - Check the checkbox of your topic
   - Click on the `Show Info Panel` button
   - Select `Permissions`
   - Click on `Add Permissions`
   - Add [gmail-api-push@system.gserviceaccount.com](mailto:gmail-api-push@system.gserviceaccount.com) and select the Pub/Sub Publisher role
   - Screenshots

   <div class="docs-screenshot-compact">

   ![perm 1.png](./assets/perm-1.png)

   </div>

   <div class="docs-screenshot-compact">

   ![perm 2.png](./assets/perm-2.png)

   </div>

   <div class="docs-screenshot-compact">

   ![perm 3.png](./assets/perm-3.png)

   </div>

   <div class="docs-screenshot-compact">

   ![perm 4.png](./assets/perm-4.png)

   </div>

5. Create a HTTP request in Mindflow to watch for emails notifications using the credentials
   - Screenshots

   <div class="docs-screenshot-compact">

   ![Create http request mindflow watch emails](./assets/create-http-request-mindflow-watch-emails.png)

   </div>
