2013-03-01 41 views
5

尝试使用g +文档exampleAuthorize requests using OAuth 2.0: ON。得到了Unauthorized。这里输出:Google+无法插入时刻

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

试图撤销谷歌api资源管理器的权限和再次验证。没有改变。我做错了什么或g + apis尚未准备好用于生产?

回答

3

它看起来像我的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调用。

+2

好,看起来像'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

+0

@class“replacewithuniqueidforaddtarget”是什么意思,你只是在这里键入一个随机ID或什么? – Supertecnoboff 2014-06-04 11:19:52

+0

对于目标ID,它只是一个随机字符串,如你所建议的。 – class 2014-06-04 14:34:54