它看起来像我的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.plus.moments.insert代码代替gapi.client.request调用。
好,看起来像'requestvisibleactions'是问题的根源。 [此帖子](https://plus.google.com/118276561380249048216/posts/2kMX9Dzaf8V)会导致正确的[diff](https://code.google.com/p/google-api-php-client/source /diff?spec=svn533&r=533&format=side&path=/trunk/src/auth/Google_OAuth2.php)进行服务器端oauth身份验证。 感谢您的信息! – mente 2013-03-01 22:04:29
@class“replacewithuniqueidforaddtarget”是什么意思,你只是在这里键入一个随机ID或什么? – Supertecnoboff 2014-06-04 11:19:52
对于目标ID,它只是一个随机字符串,如你所建议的。 – class 2014-06-04 14:34:54