Soumya Kanti Naskar
Soumya Kanti Naskar

Reputation: 1081

Request Module returning null for google API

I was trying Gmail APIs. Using POSTMAN I have created oAuth 2.0 token and was able to hit the URL https://www.googleapis.com/gmail/v1/users/[email protected]/profile. But when I was trying same with my node project where I invoked the same using :

app.get('/getMail',getMail); in my index.js where getMail is as follows

exports.getMail =  (req, res) => {

request({
  url: "https://www.googleapis.com/gmail/v1/users/[email protected]/profile",
  method: "GET",
  headers: {
    Authorization: 'Bearer token'
  },
  json: true
}, function(response) {
  console.log(JSON.stringify(response, null, 4));
  return res.json(response);
});

But I am getting the response as null. Any help would be appreciated.

Upvotes: 0

Views: 189

Answers (1)

Sagar Chilukuri
Sagar Chilukuri

Reputation: 1448

Please change the callback function to include error. The callbacks are usually error-first callbacks meaning first argument is always error.


exports.getMail =  (req, res) => {

request({
  url: "https://www.googleapis.com/gmail/v1/users/[email protected]/profile",
  method: "GET",
  headers: {
    Authorization: 'Bearer token'
  },
  json: true
   // Here -> error
}, function(error, response) {
  if (error) throw new Error(error); // Handle the error here.
  console.log(JSON.stringify(response, null, 4));
  return res.json(response);
});

Upvotes: 3

Related Questions