2013-08-17 16 views
0

根據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開發工具裏面: enter image description here

我被期望根據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開發者工具的這張照片清除了這一點。

enter image description here enter image description here

+0

什麼是您的HTTP響應有效載荷?原始的JSON響應。 – mariuss

+0

我顯示了我的第一張圖片的回覆標籤。我如何向你展示原始的JSON響應,如果我已經包含的不是你想要的? –

+0

您只顯示標題,而不顯示正文。 – mariuss

回答

1

經過大量研究後,我通過使用$ http.jsonp()方法而不是$ http.get()方法解決了我的問題。我想我只是忽略了$ http.jsonp()方法,因爲在創建遠程腳本請求時我是個新手,而且真的不知道JSONP是什麼。

所以現在我的新代碼如下所示:

$http.jsonp('https://www.googleapis.com/oauth2/v1/tokeninfo', 
       { 
        params: { 
         access_token: accessToken.token, 
         callback: 'JSON_CALLBACK' 
        } 
       }) 
       .success(function (data, status, headers, config) { 
        // Do something successful. 
       }) 
       .error(function (data, status, headers, config) { 
        // TODO: Handle the error 
       }); 

我要感謝所有誰花時間來查看我的問題和那些回答我的問題。我希望這可以幫助像我這樣的AngularJS,JSONP和Google OAuth2的新用戶。

謝謝!

0

的主要原因奇怪的反應是存在的URL空間中的事實呢?和access_token。

我無法幫助您,因爲沒有完整的測試用例,我可以運行以重現您的問題 - 它不會在本地測試中爲我重現。

+0

我不確定你在哪裏看到URL中的空間。我沒有看到我在這裏發佈的內容。請檢查文章末尾,因爲我已更新以顯示XHR請求的Chrome開發人員工具的新圖片。正如您從高亮顯示的路徑中看到的那樣,URL中沒有空格。請解釋。謝謝。 –

+0

GET https://www.googleapis.com/oauth2/v1/tokeninfo? access_token = ya29.AHES6ZQBj6feCrXuDoqwA3FpX1T1HO8fH1eWegJbiBTp7Cs HTTP/1.1 - 中有一個空格。實際上,它看起來像一個換行符。 –

0

使用角1.2.x,這不會發生。很顯然,1.0.x的角色是Google的tokeninfo服務拒絕的預發佈的$ http請求。