【问题标题】:Accessing Google Api using Nodejs使用 Nodejs 访问 Google Api
【发布时间】:2016-11-15 07:56:42
【问题描述】:

我已经搭建了一个 Web 应用程序,它能够使用 Passportjs 和 Google Oauth2 注册用户。在范围内注册用户时,我要求访问 Google Plus API 并检索要保留在 MongoDB 中的 accessToken 和 refreshToken。

稍后在应用程序中,我尝试调用 API 来检索 Google Plus 用户配置文件进行测试。我在代码中遵循以下步骤

var oAuth2 = googleCredentials.oauthClient(); // 设置一个新的 Oauth2 // 使用项目凭据的客户端

然后检索此特定用户的 access_token 和 refresh_token 并设置如下凭据

oAuth2.setCredentials({
      access_tokens: user[0].access_token,
      refresh_tokens: user[0].refresh_token
    });

然后进行 API 调用

  plus.people.get({
      userId: 'me',
      auth: oAuth2
    }, function (err, response) {

      console.log(err);
      res.send(response);

    });

但我的应用程序错误记录了以下错误

[错误:未设置访问或刷新令牌。]

任何帮助将不胜感激

【问题讨论】:

  • 我什至通过访问 url googleapis.com/oauth2/v1/tokeninfo?access_token=[myaccess token] 检查了访问令牌的有效性并收到了肯定的响应,但真的无法理解为什么 API 调用总是报告错误 a上文提到的。非常感谢任何帮助

标签: google-api-nodejs-client


【解决方案1】:

向那些花时间阅读我的代码的人道歉,因为它显然很糟糕。设置凭据时,我传递了多个单词,例如 access_tokens 而不是 access_token 和 refresh_token。我花了一整夜试图找出问题所在。

我很高兴我能够继续前进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 2017-07-24
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2014-09-23
    相关资源
    最近更新 更多