【问题标题】:Request Module returning null for google API请求模块为谷歌 API 返回 null
【发布时间】:2020-10-16 03:12:46
【问题描述】:

我正在尝试使用 Gmail API。使用 POSTMAN 我创建了 oAuth 2.0 令牌并能够访问 URL https://www.googleapis.com/gmail/v1/users/xyz@gmail.com/profile。但是当我尝试与我的节点项目相同时,我使用以下方法调用相同的:

app.get('/getMail',getMail);在我的index.js里面getMail如下

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

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

但我得到的响应为空。任何帮助将不胜感激。

【问题讨论】:

    标签: node.js gmail-api


    【解决方案1】:

    请更改回调函数以包含error。回调通常是错误优先回调,这意味着第一个参数始终是错误的。

    
    exports.getMail =  (req, res) => {
    
    request({
      url: "https://www.googleapis.com/gmail/v1/users/xyz@gmail.com/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);
    });
    

    【讨论】:

    • 完美 :) 我无法在 11 分钟内获得答案。但我也会这样做。
    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2020-10-20
    • 2015-08-14
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多