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

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

问题描述

使用 Authorize 从 g+ 文档中尝试 example使用 OAuth 2.0 的请求:开启.结果得到 Unauthorized.这是输出:

请求发布 https://www.googleapis.com/plus/v1/people/me/moments/vault?debug=true&key={YOUR_API_KEY}内容类型:应用程序/json授权:承载 *my_token*X-JavaScript-User-Agent:Google APIs Explorer{目标": {"url": "https://developers.google.com/+/web/snippet/examples/thing"},类型":http://schemas.google.com/AddActivity"}回复401 未授权缓存控制:私有,最大年龄=0内容编码:gzip内容长度:93内容类型:应用程序/json;字符集=UTF-8日期:2013 年 3 月 1 日星期五 18:56:34 GMT到期时间:2013 年 3 月 1 日星期五 18:56:34 GMT服务器:GSEwww-authenticate: AuthSub realm="https://www.google.com/accounts/AuthSubRequest" allowed-scopes="https://www.googleapis.com/auth/plus.login,https://www.google.com/accounts/OAuthLogin"{错误": {错误":[{"message": "未经授权"}],代码":401,"message": "未经授权"}}

尝试撤销 google api explorer 权限并再次进行身份验证.没有改变.是我做错了什么还是 g+ api 还没有准备好用于生产?

解决方案

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

您需要做两件事:

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

<button class="g-signin"数据范围="https://www.googleapis.com/auth/plus.login"data-requestvisibleactions="http://schemas.google.com/AddActivity"data-clientId="YOUR_CLIENT_ID"数据回调=onSignInCallback"数据主题=暗"data-cookiepolicy="single_host_origin">

接下来,您需要构建和编写应用活动.以下示例使用 JavaScript 显示了这一点:

 var 有效载荷 = {目标": {"id" : "replacewithuniqueidforaddtarget","图片" : "http://www.google.com/s2/static/images/GoogleyEyes.png","type" : "http://schema.org/CreativeWork","description": "活动描述","name":"AddActivity 的一个例子"},"type":"http://schemas.google.com/AddActivity",开始日期":2012-10-31T23:59:59.999Z"};变量参数 = {'路径':'/plus/v1/people/me/moments/vault','方法':'POST','body': JSON.stringify(payload),'回调':函数(响应){控制台日志(响应);}};gapi.client.request(args);

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

http://wheresgus.com/appactivitiesdemo

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

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

更新

请注意,现场演示已更新为以下代码,您不应直接调用 gapi.client.request:

writeListenActivity: function(url){无功载荷 = {"type": "http://schemas.google.com/ListenActivity",}如果(网址!=未定义){payload.target = { 'url' : url };}别的{有效载荷.目标 = {"type": "http://schema.org/MusicRecording","id": "uniqueidformusictarget","description": "一首关于想念在美国内战中战斗的家人的歌曲","image": "https://developers.google.com/+/plugins/snippet/examples/song.png","name": "当约翰尼行军回家时"};}this.writeAppActivity(payload);},writeAddActivity:函数(网址){无功载荷 = {"type":"http://schemas.google.com/AddActivity",开始日期":2012-10-31T23:59:59.999Z"};如果(网址!=未定义){有效载荷.目标 = {网址":https://developers.google.com/+/plugins/snippet/examples/thing"};}别的{有效载荷.目标 = {"id" : "replacewithuniqueidforaddtarget","图片" : "http://www.google.com/s2/static/images/GoogleyEyes.png","type" : "http://schema.org/CreativeWork","description": "活动描述","name":"AddActivity 的一个例子"};}this.writeAppActivity(payload);},writeAppActivity:函数(有效载荷){gapi.client.plus.moments.insert({'用户ID':'我','集合':'保险库',资源":有效载荷}).执行(函数(结果){控制台日志(结果);});}

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

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?

解决方案

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.

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

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