1

我已經用Python編寫了幾個Google Cloud Endpoints,並且要求followed the directions要求對它們的調用來自使用Firebase進行身份驗證的用戶。我需要使用JavaScript從Web應用程序調用端點,但似乎無法使身份驗證正常工作。如何從Javascript調用Firebase身份驗證的雲端點?

我想使用Google APIs client(gapi),它具有從提供的發現文檔動態生成客戶端庫的額外好處。當我嘗試使用gapi客戶端時,我可以打電話給我的API,但我得到一個HTTP 401作爲響應,以及我的python源返回的HTTP未授權消息。

谷歌有關該主題的文檔相當稀少。我從one tutorial on the subject收集到可以使用標準的Ajax調用,但我沒有看到有關如何從Gapi調用Firebase身份驗證端點的任何文檔。我目前的擔心是,gapi客戶端可能尚未設置(尚)允許使用發現文檔,並且還允許將Authorization標頭設置爲Firebase身份驗證所需。

我試圖甚至有可能嗎?

任何建議,將不勝感激。也許使用gapi客戶端調用Firebase Authenticated端點是不可能的。

這裏是我的GAPI js代碼一個大致的輪廓:

function(token) { 
     gapi.client.init({ 
      apiKey: 'MY_API_KEY', 
      discoveryDocs: [MY_DISCOVERY_DOC_URL'], 
      clientId: 'MY_WEB_CLIENT_ID', 
      scope: 'profile' 
     }).then(function(){ 
      return gapi.client.my.server.api.call(); 
     }).then(function(response){ 
      console.log(response.result.data) 
     }, function(reason){ 
      console.log('Error: ' + reason.result.error.message) 
     }); 
    } 

回答

0

我一直在掙扎了一會兒,終於使它工作。我發現了兩個選項:

選項1)如果你想使用gapi.client庫: 有)稱爲gapi.client.setToken(tokenObject的方法 - documentation

然而,這似乎是新的(17年7月)和一些文檔或例子可用。我做了它的工作做好以下(這是與角火angularJS但我希望你我在做什麼,基本上忽略「$範圍」)

// any time auth state changes, add the user data to scope 
$scope.auth.$onAuthStateChanged(function (firebaseUser) { 
    $scope.firebaseUser = firebaseUser; 
    $scope.idToken = null; 

    // get the token from the firebase User Object 
    // Note that getToken() is deprecated and for me it did not work as desired 
    // use getIdToken() instead 
    firebaseUser.getIdToken().then(function (idToken) { 
     $scope.idToken = idToken; 
    }); 
}); 

// Now you can use setToken 
// If from the docs you were thinking firebase's getIdToken() gives me TokenObject and gapi's setToken() 
// expects a TokenObject so I'll just pass it - you'd be wrong! (at least for me - if it works for you please give me a heads up) 
// You'll need to build your own token: 
var homemadeToken = { 
    access_token: $scope.idToken.toString() // This feels so wrong 
}; 

gapi.client.setToken(homemadeToken); 
gapi.client.yourapi.getSomething().execute(function (resp) { 
    // Do stuff with the response 
    } 
); 

選項2)使用jQuery的Ajax請求 - documentation

$.ajax(backendHostUrl + '/_ah/api/yourapi/v1/someendpoint', { 
    headers: { 
     'Authorization': 'Bearer ' + $scope.idToken // Here it worked without making a string first but I did not check why 
    }, 
    method: 'GET', 
    success: function (resp) { 
     // Do stuff with the response 
    } 
}); 

如果畢竟,你的後端仍然不接受令牌,並且已經從端點V1遷移到v2,它可能會幫助migrating again as described here。 ESP。確保lib文件夾被重新創建。 即使在SDK更新之後,我注意到如果一旦從v1遷移到v2,「lib」文件夾不會更新,無論它是否已更新。

還不能正常工作? 這個github page解決了早期版本的BACKEND方面的問題 - 後端不接受Firebase標記並需要被黑客入侵。如果您想按照此處所述應用更改,並且根據遷移指南使用最新的「lib」文件夾(在'17月7月編寫)users_id_token.py,請注意文件已更改,您需要對顯式評論在該文件的_verify_signed_jwt_with_certs方法中:

# Formerly we would parse the token body here. 
# However, it's not safe to do that without first checking the signature. 

並在檢查簽名之前解析令牌。但是從該文件的評論中可以推斷出,Google計劃將其他地方的邏輯放在其他地方 - 希望Firebase友好且安全。

+0

感謝您回答這樣一個老問題。我通過使用類似於您的選項#2的Ajax調用來解決問題。也許現在我會回去使用gapi調用,因爲它似乎已經更新了庫。 – HondaGuy