【问题标题】:Retrieve Google+ activity list of an user检索用户的 Google+ 活动列表
【发布时间】:2014-07-08 16:21:00
【问题描述】:

我需要获取用户在 Google+ 中的活动列表。我的编码平台是 node.js Express 框架,我使用的是 google-api-nodejs-client 包。

var googleapis = require('googleapis');
var auth = new googleapis.OAuth2Client();
var accessToken="XXXXXX......";
googleapis
    .discover('plus', 'v1')
    .execute(function (err, client) {
        if (err) {
            console.log('Problem during the client discovery.', err);
            return;
        }
        auth.setCredentials({
            access_token: accessToken
        });
        client
            .plus.people.get({ userId: 'me' })
            .withAuthClient(auth)
            .execute(function (err, response) {
                console.log('My profile details------>', response);
            });
        client
            .plus.activities.list({
                userId: 'me',
                collection: 'public',
                maxResults: 100
            })
            .withAuthClient(auth)
            .execute(function (err, response) {
                console.log('err----------------------------->',err);//EDIT
                console.log('activities---------------------->', response.items);
            });
    });

我得到了我的个人资料详细信息。但活动正在返回值:null。我检查了我的 Google+ 页面以确保我有公开的帖子。另外,我自己也向“公众”分享了一些帖子。请帮助我找到我的代码中的错误。

编辑

其实有一个错误。我按照 Ryan Seys 的建议通过在控制台中记录 err 对象的值找到了它。

错误--------------->

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

【问题讨论】:

  • 尤其是在像这样调试时,您还应该看看“err”中返回的内容。检查并将其作为问题的一部分。

标签: express google-api google-plus google-api-nodejs-client


【解决方案1】:

如果您提供err 对象的值会有所帮助,但这里有一些想法:

  1. 您是否为您的项目启用了 Google+ API?请参阅 https://console.developers.google.com/ 以及项目的 API 和 auth 部分以启用 API。

  2. 您是否正在为用户个人资料数据请求正确的范围。请参阅https://developers.google.com/apis-explorer/#p/plus/v1/plus.activities.list 以试用该请求。单击该页面上的 OAuth 按钮以查看您可能希望尝试向用户请求的不同类型的范围。我现在看到的一些范围是:

  3. 尝试向 API 请求添加一个空的正文字段。这是当前 API 客户端的一个警告,有些请求要求您在参数对象后输入默认的空 {}

    client
        .plus.activities.list({
            userId: 'me',
            collection: 'public',
            maxResults: 100
    
        }, {}) // <--- see the extra {} here! 
    
        .withAuthClient(auth)
        .execute(function (err, response) {
            console.log('activities---------------------->', response.items);
        });
    

【讨论】:

  • 你是对的。原因是您在第二点中提到的范围。我按照你说的记录了错误值。
  • 我只会收到我添加的公开帖子。如何获取他人的公开帖子?
【解决方案2】:

我认为问题在于您为 client.plus.activities.list() 指定了一个空的 fields 参数,而不是根本不提供 fields 参数。这告诉它不为结果返回任何字段。由于 fields 参数是可选的,因此可以完全省略。

尝试类似:

    client
        .plus.activities.list({
            userId: 'me',
            collection: 'public',
            maxResults: 100
        })

【讨论】:

  • 我把它改成了你的答案。但还是没有结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多