使用Nodejs访问Google Api

时间:2016-11-14 01:04:56

标签: google-api-nodejs-client

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

稍后在应用程序中,我尝试进行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);

    });

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

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

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

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

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