2015-07-03 164 views
0

我一直在掙扎了一段時間,現在使用谷歌的OAuth 2.0認證,以獲取Google+的用戶配置文件,我從InAppBrowser訪問科爾多瓦。我已經能夠從InAppBrowser獲得響應代碼。當我想使用響應代碼獲取授權令牌時發生問題,我想使用它來訪問Google+ API。我不知道應該使用哪個URL來獲取令牌,對於client_secret,我使用的是Google Developers控制檯的憑據(Key for Android應用程序:API密鑰)中提供的那個。科爾多瓦谷歌的OAuth 2.0 - 誤差在獲得令牌

下面是代碼:

authorize: function(options) { 
    var deferred = $.Deferred(); 

    var authUrl = 'https://accounts.google.com/o/oauth2/auth?' + $.param({ 
     response_type: 'code', 
     client_id: options.client_id, 
     redirect_uri: options.redirect_uri, 
     scope: options.scope 
    }); 

    var authWindow = cordova.InAppBrowser.open(authUrl, '_blank', 'location=no,toolbar=no'); 

    $(authWindow).on('loadstart', function(e) { 
     var url = e.originalEvent.url; 
     var code = app.getParameterByName(url, 'code').trim(); 
     var error = app.getParameterByName(url, 'error'); 

     if (code || error) { 
      authWindow.close(); 

      if (code) { 
       $.post('https://accounts.google.com/o/oauth2/token', { 
        code: code, 
        client_id: options.client_id, 
        client_secret: options.client_secret, 
        redirect_uri: options.redirect_uri, 
        grant_type: 'authorization_code' 
       }).done(function(data) { 
        deferred.resolve(data); 
       }).fail(function(response) { 
        console.log(JSON.stringify(response)); 
        deferred.reject(response.responseJSON); 
       }); 
      } else if (error) { 
       deferred.reject({error: error}); 
      } 
     } 
    }); 

    return deferred.promise(); 
} 

該請求將總是被捕獲爲失敗,與響應消息: { 「readyState的」:0 「狀態」:0 「狀態文本」:「錯誤: SecurityError:DOM Exception 18「}

真的需要你的幫助。

謝謝

+0

快速猜測,嘗試卸載插件 「科爾多瓦 - 插件白名單」,然後添加「cordova-plugin-legacy-whitelist」。請不要這不是推薦的解決方案,但如果它解決了您的問題,這意味着您必須正確配置安全設置。你用什麼版本的Android測試? – QuickFix

+0

我使用安裝了android v4.0.2的cordova v5.1.1。我剛剛嘗試了您的建議,將舊白名單插件替換爲白名單插件。不幸的是結果是一樣的。答覆仍然失敗。因此,對於令牌網址,我應該使用https://www.googleapis.com/oauth2/v3/token訪問嗎?我也試過這個,但仍然失敗。有沒有我提出錯誤的請求的參數?響應錯誤消息也不夠清楚,我不能在Google上搜索。任何其他建議? –

+0

它是關於內容安全策略,如cordova v5.1.1使用cordova-plugin-whitelist,它指定:

回答

0

那麼,解決方案是在您的最新評論。您將default-src定義爲self,並且不定義connect-src,因此您的應用只能連接到當前頁面。

可以定義*或accounts.google.com作爲連接-SRC:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src *;"> 

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src 'self' https://accounts.google.com;">