2013-03-01 65 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