0

i am trying to read gmail inbox using google API in nodejs. but it return null value(messages), in this code im giving the message id directly copy from gmail inbox link

(function() {
  'use strict';

  var fs = require('fs');
  var googleAuth = require('google-auth-library');
  var google = require('googleapis');

  function getOAuth2Client(cb) {
    // Load client secrets
    fs.readFile('client_secret.json', function(err, data) {
      if (err) {
        return cb(err);
      }
      var credentials = JSON.parse(data);
      var clientSecret = credentials.installed.client_secret;
      var clientId = credentials.installed.client_id;
      var redirectUrl = credentials.installed.redirect_uris[0];
      var auth = new googleAuth();
      var oauth2Client = new auth.OAuth2(clientId, clientSecret,     redirectUrl);

      // Load credentials
      fs.readFile('gmail-credentials.json', function(err, token) {
        if (err) {
          return cb(err);
        } else {
          oauth2Client.credentials = JSON.parse(token);
          return cb(null, oauth2Client);
        }
      });
    });
  }


  function getMessage(auth) {
  var gmail = google.gmail({ auth: auth, version: 'v1' });

  gmail.users.messages.get({
    'userId': 'me',
    'id': '153a1f810aece662'
  }, function (err, result) {
    console.log(result);
  });
}    

  getOAuth2Client(function(err, oauth2Client) {
    if (err) {
      console.log('err:', err);
    } else {
      console.log(oauth2Client);
      getMessage(oauth2Client, function(err, results) {
        if (err) {
          console.log('err:', err);
        } else {
          console.log(results);
        }
      });    
    }
  });
})();

the OAuth authentication data is perfectly worked but the null is return

1 Answers1

0

First output the error you have, that will help to debug the problem:

gmail.users.messages.get({
    'userId': 'me',
    'id': '153a1f810aece662'
  }, function (err, result) {
  if(err) console.log('Error', err);
  console.log(result);
});
shershen
  • 9,875
  • 11
  • 39
  • 60
  • Error { [Error: Insufficient Permission] code: 403, errors: [ { domain: 'global', reason: 'insufficientPermissions', message: 'Insufficient Permission' } ] } – varadharajan p.r Rajan Mar 24 '16 at 05:55
  • so as you can see you have "Insufficient Permission" in your Gmail API, check those: http://stackoverflow.com/questions/32143126/how-do-i-get-around-httperror-403-insufficient-permission-gmail-api-python, http://stackoverflow.com/questions/30661489/gmail-api-insufficient-permission – shershen Mar 24 '16 at 08:45
  • what are things i need to learn in node API ...please help me – varadharajan p.r Rajan Mar 29 '16 at 05:38