根據documentation發送Google OAuth2令牌驗證請求後,我在閱讀JSON響應數據時遇到問題。需要幫助驗證令牌 - 我無法讀取JSON響應數據
下面是谷歌的OAuth2令牌驗證請求:
$http.get('https://www.googleapis.com/oauth2/v1/tokeninfo', {params: {access_token: accessToken.token}})
.success(function (data, status, headers, config) {
$log.info("sendValidationRequest() method: data: " + data);
deferred.resolve(data);
accessToken.state = getSession().state = $window.sessionStorage.state = "AccessTokenValidated";
})
.error(function (data, status, headers, config) {
//handle error
});
所以,當我檢查該JSON響應數據對象,我得到如下:
data.audience; // undefined
data.scope; // undefined
data.userid; // undefined
data.expires_in; //undefined
這就是JSON響應數據看起來像在Chrome開發工具裏面:
我被期望根據documentation將JSON響應數據設置爲JSON數組。 JSON響應數據似乎被編碼。我該如何解決這個問題?
這裏是我的HTTP頭請求:
GET https://www.googleapis.com/oauth2/v1/tokeninfo?
access_token=ya29.AHES6ZQBj6feCrXuDoqwA3FpX1T1HO8fH1eWegJbiBTp7Cs HTTP/1.1
:host: www.googleapis.com
origin: http://localhost:8080
accept-encoding: gzip,deflate,sdch
accept-language: en-US,en;q=0.8
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
:path: /oauth2/v1/tokeninfo? access_token=ya29.AHES6ZQBj6feCrXuDoqwA3FpX1T1HO8fH1eWegJbiBTp7Cs
accept: application/json, text/plain, */*
:version: HTTP/1.1
referer: http://localhost:8080/calendar-app/
:scheme: https
:method: GET
這裏是我的HTTP響應頭:
HTTP/1.1 200 OK
status: 200 OK
version: HTTP/1.1
access-control-allow-origin: http://localhost:8080
access-control-expose-headers: Cache-Control,Content-Encoding,Content-Length,Content-Type,Date,Expires,Pragma,Server
cache-control: no-cache, no-store, max-age=0, must-revalidate
content-encoding: gzip
content-length: 182
content-type: application/json; charset=UTF-8
date: Sat, 17 Aug 2013 08:00:05 GMT
expires: Fri, 01 Jan 1990 00:00:00 GMT
pragma: no-cache
server: GSE
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
與解決我的問題的任何幫助深表感謝。
謝謝。
經過一番研究,我發現這是AngularJS $ http服務特有的問題;因爲這不是JQuery的問題。這裏是我的驗證的訪問令牌代碼看起來像JQuery的:
function validateToken() {
$.ajax({
url: 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + accessToken,
data: null,
success: function(response){
console.log('Our token is valid. and response.audience value is defined.');
},
error: function(error) {
console.log('Our token is not valid....');
},
dataType: "jsonp"
});
}
jQuery的Ajax請求頭是不同的請求頭是從$ HTTP服務請求頭不同(見上文):
GET https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ya29.AHES6ZTewZF3-aEJ3p8mdJahvTtZDNELYjpqLsGaB10Fnmk&callback=jQuery172036758901784196496_1376731301308&_=1376731301856 HTTP/1.1
:host: www.googleapis.com
accept-encoding: gzip,deflate,sdch
accept-language: en-US,en;q=0.8
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
:path: /oauth2/v1/tokeninfo?access_token=ya29.AHES6ZTewZF3-aEJ3p8mdJahvTtZDNELYjpqLsGaB10Fnmk&callback=jQuery172036758901784196496_1376731301308&_=1376731301856
accept: */*
:version: HTTP/1.1
referer: http://localhost:8080/calendar-app/google-oauth-test2.html
:scheme: https
:method: GET
當$ HTTP服務發出請求時,Google Response響應頭會有所不同(參見上文):
HTTP/1.1 200 OK
status: 200 OK
version: HTTP/1.1
cache-control: no-cache, no-store, max-age=0, must-revalidate
content-encoding: gzip
content-length: 235
content-type: text/javascript; charset=UTF-8
date: Sat, 17 Aug 2013 09:21:40 GMT
expires: Fri, 01 Jan 1990 00:00:00 GMT
pragma: no-cache
server: GSE
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
所以,現在的問題就變成了,如何讓AngularJS $ http服務請求標頭匹配JQuery的AJAX請求標頭?或者有另一種方法可以獲得相同的結果;這是一個成功的訪問令牌驗證後,從谷歌獲得適當的JSON數組對象?
再次,任何幫助,這是非常感謝!
我正在更新帖子,因爲似乎有一些問題是否在URL中有空格,而且我沒有顯示原始JSON響應。我希望來自chrome開發者工具的這張照片清除了這一點。
什麼是您的HTTP響應有效載荷?原始的JSON響應。 – mariuss
我顯示了我的第一張圖片的回覆標籤。我如何向你展示原始的JSON響應,如果我已經包含的不是你想要的? –
您只顯示標題,而不顯示正文。 – mariuss