Google+无法插入时刻

时间:2013-03-01 19:00:30

标签: oauth google-plus

使用Authorize requests using OAuth 2.0: ON从g +文档中尝试example。得到Unauthorized作为结果。这是输出:

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"
 }
}

试图撤销google api资源管理器权限并再次进行身份验证。没有改变。我做错了什么还是g + apis尚未准备好用于生产?

1 个答案:

答案 0 :(得分:3)

在我看来,API浏览器目前无法将应用程序活动编写到Google,因为它没有将requestvisibleactions字段传递给OAUTH2流程。您仍然可以手动执行操作,如下所述。

您需要做两件事:

首先,确保您正在呈现登录按钮,其中为您要插入的应用活动类型设置了requestvisibleactions。以下示例显示了如何使用add活动呈现登录:

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

接下来,您需要构建和编写应用程序活动。以下示例使用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);

您可以在此处观看现场演示:

http://wheresgus.com/appactivitiesdemo

您可以在此处的文档中了解所有活动类型:

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

<强>更新

请注意,现场演示已使用以下代码进行更新,您不应直接调用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);
      });
}

特别值得注意的是用于替换gapi.client.request调用的gapi.client.plus.moments.insert代码。