Inserting google plus moment using googleapis node.js api (google-api-nodejs-client)

白昼怎懂夜的黑 提交于 2019-12-13 06:05:21

问题


Using the example here: https://developers.google.com/+/api/latest/moments/insert

and everyauth to handle the oauth login:

(with the .withAuth() added)

Or using my own simpler example I'm getting an "Invalid Value" status code: 400 error.

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

Has anyone been able to post a Google + moment using the google-api-nodejs-client library?

I use everyauth to handle oauth and login. My initialization code is below:

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('/');

The code above produces the following url:

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

Note: I am using the "request-visible-actions" parameter in the url. Also note that the moment type matches the type specified in the code below as well as the schema.org specification for that type of moment.

The scopes seem to be processed correctly. The login sequence prompts me to approve

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:...

My code:

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

I have not had any problems interacting with Google+ or making authorized requests to Google services otherwise but can't seem to get the Google+ moments to insert. Has anyone been able to insert a Google+ moment using the google-api-nodejs-client library? If so, please tell me what I'm doing wrong.


回答1:


i've ran into a similar problem with the youtube api and it seems that way we should call the api from the node library is not the same as specified on the api docs.

specifically instead of doing this:

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

you have to make the following call:

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

the parameters of the requests are passed in the first parameter, and the request body is passed in the second parameter of the function.




回答2:


I've been able to get this example to work using the same exact moment object and OAuth URL using Google's API in Python.

It appears there is some bug in how google-api-nodejs-client is packaging my request to insert a moment.

I don't have time to examine further so I'm going to stick with Python.

I would still prefer to use Node. If anyone comes out with a bug fix or a solution to make this work in the Node api please let me know.



来源:https://stackoverflow.com/questions/20492925/inserting-google-plus-moment-using-googleapis-node-js-api-google-api-nodejs-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!