0

使用此处的示例:https ://developers.google.com/+/api/latest/moments/insert

和everyauth 来处理oauth 登录:

(添加了 .withAuth() )

或者使用我自己更简单的示例,我收到“无效值”状态代码:400 错误。

{"domain":"global","reason":"invalid","message":"Invalid Value"}

有没有人能够使用 google-api-nodejs-client 库发布 Google + 时刻?

我使用everyauth 来处理oauth 和登录。我的初始化代码如下:

everyauth.google
  .appId(conf.googlehybrid.clientId)
  .appSecret(conf.googlehybrid.clientSecret)
  .scope('https://www.googleapis.com/auth/plus.login\
          https://www.googleapis.com/auth/plus.moments.write')
  .findOrCreateUser( function (sess, accessToken, extra, googleUser) {
    googleUser.refreshToken = extra.refresh_token;
    googleUser.expiresIn = extra.expires_in;
    return usersByGoogleId[googleUser.id] || (usersByGoogleId[googleUser.id] = addUser('google', googleUser));
  })
  .redirectPath('/');

上面的代码生成以下网址:

https://accounts.google.com/o/oauth2/auth?client_id=507792786278.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fgoogle%2Fcallback&response_type=code&access_type=offline&approval_prompt=auto&scope=https% 3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.login%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.moments.write&request_visible_actions=http:%2F%2Fschemas.google.com%2FCreateActivity

注意:我在 url 中使用“request-visible-actions”参数。另请注意,矩类型与下面代码中指定的类型以及该类型矩的 schema.org 规范相匹配。

范围似乎已正确处理。登录顺序提示我批准

This app would like to:
   -Know your basic profile info and list of people in your circles. (Edit list)    
   -Make your creative activity available via Google, visible to:...

我的代码:

var moment = {
  "type":"http://schemas.google.com/CreateActivity",
  "target": {
    "id": "target-id-2",
    "type": "http://schema.org/CreativeWork",
    "name": "Test moment",
    "description": "This is a sample moment",
    "text": "samplpe g+ posting!"
  }
}


gapi.client.plus.moments.insert(
  {  
     'userId': 'me',
     'collection': 'vault',
     'resource': moment
  })
 .withAuthClient(authObject)
 .execute(callback);
}

我在与 Google+ 交互或向 Google 服务发出授权请求时没有遇到任何问题,但似乎无法插入 Google+ 时刻。有没有人能够使用 google-api-nodejs-client 库插入 Google+ 时刻?如果是这样,请告诉我我做错了什么。

4

2 回答 2

1

我在 youtube api 上遇到了类似的问题,似乎我们应该从节点库调用 api 的方式与 api 文档中指定的方式不同。

具体而不是这样做:

gapi.client.plus.moments.insert(
  {  'userId' : 'me',
     'collection' : 'vault',
     'resource' : payload
  }
).execute(function(result){
  console.log(result);
});

您必须拨打以下电话:

gapi.client.plus.moments.insert(
  {  'userId' : 'me', 'collection' : 'vault'},
  payload
).execute(function(result){
  console.log(result);
});

请求的参数在第一个参数中传递,请求体在函数的第二个参数中传递。

于 2013-12-30T22:42:43.257 回答
0

我已经能够使用 Python 中的 Google API 使用相同的精确时刻对象和 OAuth URL 来让这个示例工作。

google-api-nodejs-client 打包我的请求以插入片刻的方式似乎存在一些错误。

我没有时间进一步研究,所以我将坚持使用 Python。

我仍然更喜欢使用 Node.js。如果有人提出了一个错误修复或解决方案来使其在 Node api 中工作,请告诉我。

于 2013-12-11T19:51:31.533 回答