3

I've created a topic, subscribed to it, set publishing rights of the topic using Google's API Explorer and now need to create a watch request, as described here: https://developers.google.com/gmail/api/guides/push

However, according to previous threads, you cannot do this with API Explorer and must do it directly from gcloud. I know the general form of the call is something like:

POST "https://www.googleapis.com/gmail/v1/users/me/watch"
Content-type: application/json

{
  topicName: "projects/myproject/topics/mytopic",
  labelIds: ["INBOX"],
}

However, I'm not sure exactly how to implement this in node.js - what would the code look like? I've tried the following but I get a function undefined error:

gcloud.watch({  "topicName":
"projects/pipedrivesekoul/topics/my-new-topic",  "labelIds": [  
"INBOX"  ] })

Any help much appreciated!

Sekoul
  • 1,361
  • 2
  • 14
  • 30

3 Answers3

4

yep you need to use Gmail API to setup watcher, see example below. You need to setup "oauth2Client" before doing this by yourself, i assume you have it. If you use login with google feature on your website, you already have it.

var options = {
    userId: 'me',
    auth: oauth2Client,
    resource: {
        labelIds: ['INBOX'],
        topicName: 'projects/id/topics/messageCenter'
    }
};

gmail.users.watch(options, function (err, res) {
    if (err) {
        // doSomething here;
        return;
    }
    // doSomething here;
});
Rantiev
  • 2,121
  • 2
  • 32
  • 56
  • 2
    Thank you, I was looking in all the documentation to the `resource` key and couldn't find the smallest clue that `labelIds` and `topicName ` should be in `resource` key! – Daniel Krom Jun 08 '16 at 08:57
  • Any time, glad that this helped. – Rantiev Jun 26 '16 at 10:57
  • @Sekoul can you give me your attempt to refer.I stuck with this. also i raised a issue here http://stackoverflow.com/questions/40057253/watch-requset-in-gmail-api-doesnt-work – Januka samaranyake Oct 15 '16 at 13:02
2

Unfortunately it's not supported by gcloud-node. You can use gmail API from API explorer at: https://developers.google.com/apis-explorer/#p/gmail/v1/

Takashi Matsuo
  • 3,406
  • 16
  • 25
2

If all else fails, you could simply do a POST-request with e.g. the request module, like so:

var request = require('request');

request({
  url: "https://www.googleapis.com/gmail/v1/users/me/watch",
  method: "POST",
  headers: {
    Authorization: 'Bearer {YOUR_API_KEY}'
  },
  data: {
    topicName: "projects/pipedrivesekoul/topics/my-new-topic",
    labelIds: ["INBOX"]
  },
  json: true
}, function(response) {
  console.log(JSON.stringify(response, null, 4));
});
Tholle
  • 108,070
  • 19
  • 198
  • 189