在给定访问令牌和刷新令牌的情况下,如何获取Google +用户个人资料详细信息

时间:2017-11-17 05:45:22

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

使用谷歌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范围。

1 个答案:

答案 0 :(得分:0)

问题的主要部分是您使用的是错误的端点https://developers.google.com/oauthplayground不是您可以用于我的知识的

你应该怎么做

尝试添加以下scope

  

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

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

  

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

UserInfo响应

{
  "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

进行测试