4

I am using node.js in a server to access a user's Gmail Inbox. The OAuth2 part works great, but when I try to specify the INBOX to get a message list, it seems to be ignored. I get the complete array of message ids, not just ones in the INBOX. In every attempt I get an array of the first 100 message ids, but the INBOX has only 5 messages. I've also tried with other labels such as 'UNREAD' with the same results.

It seems as though the 'labelIds' parameter is not being passed in the request (or is being ignored). Keep in mind that this is using the Node.js API without using Express.js.

Here are code snippets. What am I missing?

var http = require("http");
var url = require("url");
var fs = require('fs')
var google = require('googleapis');
var googleAuth = require('google-auth-library');
...
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

var gmail = google.gmail('v1');
gmail.users.messages.list ({
    auth:oauth2Client,
    userId:'me',
    labelIds:"INBOX"}, function (err, result)
        {
        ... etc.

1 Answers1

0

User.message.list takes a label id. So first we search the users labels. Link

GET https://www.googleapis.com/gmail/v1/users/me/labels?fields=*&key={YOUR_API_KEY}

Response:

  {
   "id": "INBOX",
   "name": "INBOX",
   "messageListVisibility": "hide",
   "labelListVisibility": "labelShow",
   "type": "system"
  },

So now we know the correct value to pass is INBOX link

GET https://www.googleapis.com/gmail/v1/users/me/messages?labelIds=INBOX&fields=*&key={YOUR_API_KEY}

Your code uses inbox when the proper value to pass is INBOX. You say you have tried this i suggest you try again and edit your question and post any errors you using INBOX should work.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449