【问题标题】:How can I get google+ user profile details, given the access token and refresh token给定访问令牌和刷新令牌,我如何获取 google+ 用户个人资料详细信息
【发布时间】:2017-11-17 05:45:22
【问题描述】:

使用谷歌 API

function googleLogin(req, res, next) {

    var google = require('googleapis');
    var plus = google.plus('v1');
    SCOPES = 'https://mail.google.com';
    var OAuth2 = google.auth.OAuth2;
    var oauth2Client = new OAuth2(
        '215995260545-6rp2pg69olionsiugudcl4fni3o36ap9.apps.googleusercontent.com',
        'xxxxxxxxxxxxxxxxxxxxxxxx',
        'https://developers.google.com/oauthplayground'
    );

    oauth2Client.setCredentials({
        access_token: req.query.ac_token,
        refresh_token: req.query.rf_token
    });
    plus.people.get({
        userId: 'me',
        auth: oauth2Client
    }, function(err, response) {
        if (err) console.log(err);
        console.log(response);
    });
}

当我运行此代码时,我收到类似**insufficient permission** 的错误。我哪里错了??

 "code": 403,
    "errors": [
        {
            "domain": "global",
            "reason": "insufficientPermissions",
            "message": "Insufficient Permission"
        }
    ]

我该如何解决这个问题?我还提到了 Gmail API 范围。

【问题讨论】:

  • 如何确认访问令牌的范围?您可以使用此 curl 命令确认检索到的访问令牌中包含的范围吗? curl -d "access_token=### your access token ###" https://www.googleapis.com/oauth2/v3/tokeninfo 这个 curl 命令可以获取访问令牌的信息。通过这个,您可以知道您的访问令牌是否包含https://www.googleapis.com/auth/userinfo.profile 以检索基本配置文件信息。如果这对解决您的问题没有帮助,我很抱歉。

标签: node.js google-api google-oauth google-api-nodejs-client


【解决方案1】:

您的问题的主要部分是您使用了错误的端点https://developers.google.com/oauthplayground,据我所知,您无法使用

你应该怎么做

尝试添加以下scope

个人资料 - 查看您的基本个人资料信息

你也不应该去通过 google+ api,你可以从 userinfo 端点请求信息,但这取决于你所追求的信息

https://www.googleapis.com/oauth2/v3/userinfo?access_token=XXX

用户信息响应

{
  "family_name": "Lawton", 
  "name": "Linda Lawton", 
  "picture": "https://lh5.googleusercontent.com/-a1CWlFnA5xE/AAAAAAAAAAI/AAAAAAAAl1I/UcwPajZOuN4/photo.jpg", 
  "locale": "en", 
  "gender": "female", 
  "link": "https://plus.google.com/+LindaLawton", 
  "given_name": "Linda", 
  "id": "117200475532672775346"
}

人员 api 请求

https://www.googleapis.com/plus/v1/people/me

回应

{
  "braggingRights": "Extreme Beekeeper first to recorded an Hive inspection using Google Glass with out a veil on.", 
  "image": {
    "url": "https://lh5.googleusercontent.com/-a1CWlFnA5xE/AAAAAAAAAAI/AAAAAAAAl1I/UcwPajZOuN4/photo.jpg?sz=50", 
    "isDefault": false
  }, 
  "id": "117200475532672775346", 
  "objectType": "person", 
  "verified": false, 
  "tagline": "Google Developer Expert 2014 - 2017", 
  "etag": "\"ucaTEV-ZanNH5M3SCxYRM0QRw2Y/0gZZ-LRb-PWLjbw12lr-IOAearE\"", 
  "circledByCount": 2514, 
  "occupation": "Google Developer Expert, BIA Developer at Targit", 
  .....  Lots of stuff here ...
  }

您应该尝试使用 Oauth playground 进行测试

【讨论】:

  • 我把它改成了你的答案。但还是没有结果
  • 您是否通过身份验证重新运行。用户现在必须授予您访问个人资料的权限。
  • function googleLogin(req, res, next) { var google = require('request'); google('https://www.googleapis.com/plus/v1/people/me?access_token=' + req.query.ac_token, function(err, account) { if (err || account.statusCode != 200) { return console.log('Invalid access token'); } console.log("ok"); }); }
  • 我已经完全改变了这个,但它给出了一个错误invalid access token
  • Insufficient Permission 意味着您通过身份验证的用户无权发出您正在发出的请求。这是您的身份验证问题。
猜你喜欢
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
  • 2014-04-19
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多