Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

oauth - Google+ unable to insert moment

Trying example from g+ documentation with Authorize requests using OAuth 2.0: ON. Got Unauthorized as result. Here's output:

Request

POST https://www.googleapis.com/plus/v1/people/me/moments/vault?debug=true&key={YOUR_API_KEY}

Content-Type:  application/json
Authorization:  Bearer *my_token*
X-JavaScript-User-Agent:  Google APIs Explorer

{
 "target": {
  "url": "https://developers.google.com/+/web/snippet/examples/thing"
 },
 "type": "http://schemas.google.com/AddActivity"
}

Response


401 Unauthorized

cache-control:  private, max-age=0
content-encoding:  gzip
content-length:  93
content-type:  application/json; charset=UTF-8
date:  Fri, 01 Mar 2013 18:56:34 GMT
expires:  Fri, 01 Mar 2013 18:56:34 GMT
server:  GSE
www-authenticate:  AuthSub realm="https://www.google.com/accounts/AuthSubRequest" allowed-scopes="https://www.googleapis.com/auth/plus.login,https://www.google.com/accounts/OAuthLogin"

{
 "error": {
  "errors": [
   {
    "message": "Unauthorized"
   }
  ],
  "code": 401,
  "message": "Unauthorized"
 }
}

Tried to revoke google api explorer permissions and authenticated again. Nothing changed. Am I doing something wrong or g+ apis are not yet ready for production use?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks to me like the APIs explorer currently will not work with writing app activity to Google because it isn't passing the requestvisibleactions field to the OAUTH2 flow. You can still do things manually as I'll describe below.

There are two things you need to do:

First, ensure that you are rendering the sign-in button with requestvisibleactions set for the app activity types you are inserting. The following example shows how you would render sign-in with the add activity:

<div id="gConnect">
  <button class="g-signin"
      data-scope="https://www.googleapis.com/auth/plus.login"
      data-requestvisibleactions="http://schemas.google.com/AddActivity"
      data-clientId="YOUR_CLIENT_ID"
      data-callback="onSignInCallback"
      data-theme="dark"
      data-cookiepolicy="single_host_origin">
  </button>
</div>

Next, you will need to construct and write the app activity. The following example shows this using JavaScript:

  var payload = {
    "target": {
      "id" : "replacewithuniqueidforaddtarget",
      "image" : "http://www.google.com/s2/static/images/GoogleyEyes.png",
      "type" : "http://schema.org/CreativeWork",
      "description" : "The description for the activity",
      "name":"An example of AddActivity"
    },
    "type":"http://schemas.google.com/AddActivity",
    "startDate": "2012-10-31T23:59:59.999Z"
  };
  var args = {
    'path': '/plus/v1/people/me/moments/vault',
    'method': 'POST',
    'body': JSON.stringify(payload),
    'callback': function(response) {
       console.log(response);
     }
  };

  gapi.client.request(args);

You can see a live demo here:

http://wheresgus.com/appactivitiesdemo

You can learn about all of the activity types from the documentation here:

https://developers.google.com/+/api/moment-types

Update

Note the live demo has been updated with the following code and you should not be directly calling gapi.client.request:

writeListenActivity: function(url){
  var payload = {
    "type": "http://schemas.google.com/ListenActivity",
  }

  if (url != undefined){
    payload.target = { 'url' : url };
  }else{
    payload.target = {
      "type": "http://schema.org/MusicRecording",
      "id": "uniqueidformusictarget",
      "description": "A song about missing one's family members fighting in the American Civil War",
      "image": "https://developers.google.com/+/plugins/snippet/examples/song.png",
      "name": "When Johnny Comes Marching Home"
    };
  }
  this.writeAppActivity(payload);
},
writeAddActivity: function(url){
  var payload = {
    "type":"http://schemas.google.com/AddActivity",
    "startDate": "2012-10-31T23:59:59.999Z"
  };
  if (url != undefined){
    payload.target = {
      'url' : 'https://developers.google.com/+/plugins/snippet/examples/thing'
    };
  }else{
    payload.target = {
      "id" : "replacewithuniqueidforaddtarget",
      "image" : "http://www.google.com/s2/static/images/GoogleyEyes.png",
      "type" : "http://schema.org/CreativeWork",
      "description" : "The description for the activity",
      "name":"An example of AddActivity"
    };
  }
  this.writeAppActivity(payload);
},
writeAppActivity: function(payload){

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

Of particular note is the gapi.client.plus.moments.insert code that replaces the gapi.client.request call.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...