Google+无法插入时刻 [英] Google+ unable to insert moment

查看:62
本文介绍了Google+无法插入时刻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在带有Authorize requests using OAuth 2.0: ON的g +文档中尝试示例.结果为Unauthorized.输出如下:

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

试图撤消google api资源管理器权限,然后再次进行身份验证.没有改变.我是在做错什么还是g + api尚未准备好用于生产?

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?

推荐答案

在我看来,API资源管理器目前无法与将应用活动写入Google一起使用,因为它没有将requestvisibleactions字段传递给OAUTH2流.您仍然可以手动执行操作,如下所述.

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.

您需要做两件事:

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

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>

接下来,您将需要构造和编写应用程序活动.以下示例使用JavaScript展示了这一点:

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

更新

请注意,实时演示已使用以下代码进行了更新,您不应直接调用gapi.client.request:

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

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

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

这篇关于Google+无法插入时刻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆