node.js - 给定访问 token 和刷新 token ,如何获取 google+ 用户个人资料详细信息

标签 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);
    });
}

当我运行此代码时,我收到类似 **权限不足** 的错误。我有什么地方错了吗?

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

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

最佳答案

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

你应该如何做

尝试添加以下内容scope

profile - View your basic profile info

您也不应该通过 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 进行测试

关于node.js - 给定访问 token 和刷新 token ,如何获取 google+ 用户个人资料详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47344025/

相关文章:

javascript - Node - ExpressJS - 如何查看 URL 请求中的所有参数和值?

firebase - 添加链接到我的 Actions on Google 应用的帐户

javascript - 如何遍历 JSON 对象定位特定属性并将其内容推送到数组?

python-2.7 - 如何通过 REST API 将文件上传到我自己的 Google 云端硬盘

java - Android GoogleAPIClient-NoClassDefFoundError : com/google/android/gms/internal/zzsa

google-chrome - 谷歌浏览器翻译页面不工作

ssl - Google oauth SSL 证书错误

c# - 访问 Google Drive Api 时部署后访问被拒绝

node.js - Node JS 环境变量和 Heroku 部署

javascript - 如何使用 Moment.js 获取一个月中的天数列表