有没有人能够让 google-api-nodejs-client 成功插入片刻?

无论我尝试什么,我都会收到通用 400“无效值”错误,但无法缩小无效值的范围,因为 API浏览器 也不行。

是因为缺少 data-requestvisibleactions 参数吗?我正在使用 Passport.js require('passport-google-oauth').OAuth2Strategy 用于处理 oauth 访问,该部分工作正常,但我不知道如何将 requestvisibleactions 合并到 oauth 请求流中,因为这绝对不是源自客户端表单。

这是我正在尝试做的事情的片段(使用最新版本 googleapis, ,v1.0.2):

var google = require('googleapis')
var auth = new google.auth.OAuth2()
auth.setCredentials({
  'access_token': user.token
})

google.plus('v1').moments.insert({
  collection: 'vault',
  userId: 'me',
  debug: true,
  resource: {
    type: "http://schemas.google.com/AddActivity",
    target: {
      type: "http://schema.org/CreativeWork",
      url: "...omitted...",
      image: "...omitted...",
      description: "test",
      name: "test"
    }
  },
  auth: auth
}, function (err, response) {
  if (err) {
    console.error(err)
    res.send(err.code, err)
  } else {
    console.log(response)
    res.send(200)
  }
})

参考文献 1 (已过时)旧版本的 googleapis)

参考文献 2 (客户端,其中 data-requestvisibleactions 的使用更为明显)

有帮助吗?

解决方案

正如您推测的那样,您需要 request_visible_actions 参数作为调用 oauth 端点的 URL 的一部分。

看起来当前版本的passport-google-oauth不支持这个参数。从几个未解决的问题和拉取请求来看,目前还不清楚作者是否会回应添加它的请求。您有两种可能的选择:

  1. 切换到使用 google-api-nodejs-client 中包含的 OAuth 支持

  2. 修补 Passport-google-oauth 代码。(并且可能会提交一个拉取请求,希望对其他人有用。)

我不使用passport.js或有问题的passport模块,所以我无法测试这个,但基于github存储库,我认为你可以插入以下内容 lib/passport-google-oauth/oauth2.js 第 136 行之后、return 语句之前:

if (options.requestVisibleActions) {
  // Space separated list of allowed app actions
  // as documented at:
  //  https://developers.google.com/+/web/app-activities/#writing_an_app_activity_using_the_google_apis_client_libraries
  //  https://developers.google.com/+/api/moment-types/
  params['request_visible_actions'] = options.requestVisibleActions;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top