5

尝试使用 g+ 文档中的示例Authorize requests using OAuth 2.0: ON。结果得到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 explorer 权限并再次进行身份验证。没有改变。我做错了什么还是 g+ api 还没有准备好用于生产?

4

1 回答 1

3

在我看来,API 资源管理器目前无法将应用程序活动写入 Google,因为它没有将 requestvisibleactions 字段传递给 OAUTH2 流。正如我将在下面描述的那样,您仍然可以手动执行操作。

您需要做两件事:

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

<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 代码。

于 2013-03-01T21:23:17.017 回答