我有一个Google+应用,可以成功为某些测试帐户编写应用活动,但其他人则返回401 Unauthorized错误:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "unauthorized",
"message": "Unauthorized"
}
],
"code": 401,
"message": "Unauthorized"
}
}
我在响应标题中也注意到了这一点:
WWW-AuthenticateBearer realm="https://www.google.com/accounts/AuthSubRequest", error=invalid_token
这似乎表明一个无效的令牌,但我不确定我是如何错误地使用gapi.auth.authorize ...特别是因为该脚本使用某些测试帐户完美地运行并且在没有问题的情况下将时刻写入G +。如果有人可以提出任何理由,某些测试帐户可能无法通过编写应用程序活动进行身份验证(或者下面的代码有任何问题),请告诉我们!
// first call gapi.auth.authorize with immediate:true:
_checkAuth = function _checkAuth(){
gapi.auth.authorize({
client_id : 'XXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com',
scope : 'https://www.googleapis.com/auth/plus.login',
request_visible_actions : 'http://schemas.google.com/CreateActivity',
immediate : true
}, function(authResult){
if(!authResult || authResult.error){
_signIn();
}else{
_performAction();
}
});
},
// if not logged in, call gapi.auth.authorize with immediate:false:
_signIn = function _signIn(){
gapi.auth.authorize({
client_id : 'XXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com',
scope : 'https://www.googleapis.com/auth/plus.login',
request_visible_actions : 'http://schemas.google.com/CreateActivity',
immediate : false
}, function(token){
gapi.auth.setToken(token);
_performAction();
});
},
// create activity
_performAction = function _performAction(){
gapi.client.load('plus','v1', function(){
gapi.client.setApiKey('XXXXXXXXXXXXXXXXXXXXXXX');
var payload = {
"type" : 'http://schemas.google.com/CreateActivity'
};
payload.target = {
"id" : "myappid",
"image" : "http://www.example.com/xxxxxxxxxxx.jpg",
"type" : 'http://schema.org/CreativeWork',
"description" : "description of activity",
"name" : "name of activity"
};
var args = {
'path' : '/plus/v1/people/me/moments/vault',
'method' : 'POST',
'body' : JSON.stringify(payload),
'callback' : function(response) {
console.log(response); // error
}
};
// triggers 401 error for some accounts
gapi.client.request(args);
});
},
答案 0 :(得分:1)
关于原因的几点想法:
请求对演示中使用的应用活动进行写访问的示例按钮:
<button class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login"
data-requestvisibleactions=
"http://schemas.google.com/AddActivity http://schemas.google.com/ListenActivity"
data-clientId="268858962829.apps.googleusercontent.com"
data-callback="onSignInCallback"
data-theme="dark"
data-cookiepolicy="single_host_origin">
</button>
使用客户端库编写应用程序活动的示例。
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);
});
}
工作演示: