gpt4 book ai didi

oauth - Google+ 无法插入时刻

转载 作者:行者123 更新时间:2023-12-04 20:47:26 25 4
gpt4 key购买 nike

正在尝试 example来自 Authorize requests using OAuth 2.0: ON 的 g+ 文档.收到 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 还没有准备好用于生产?

最佳答案

在我看来,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.plus.moments.insert 代码,它替换了 gapi.client.request 调用。

关于oauth - Google+ 无法插入时刻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15164949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com