如何从 Javascript 调用经过 Firebase 身份验证的云端点?
How to Call a Firebase Authenticated Cloud Endpoint from Javascript?
我在 Python 中编写了几个 Google 云端点,并且 followed the directions 要求对它们的调用来自使用 Firebase 进行身份验证的用户。我需要使用 JavaScript 从 Web 应用程序调用我的端点,但我似乎无法进行身份验证。
我想使用 Google APIs client (gapi),它具有从提供的发现文档动态生成客户端库的额外好处。当我尝试使用 gapi 客户端时,我可以很好地调用我的 API,但我得到一个 HTTP 401 作为响应,以及我的 python 来源 [=36] 的 HTTP 未授权消息=].
Google 关于这个主题的文档相当稀少。我从 one tutorial on the subject 了解到可以使用标准 Ajax 调用,但我没有看到任何关于如何从 Gapi 调用经过 Firebase 身份验证的端点的文档。我目前担心的是,gapi 客户端可能(尚未)设置为允许使用发现文档,并且还允许根据 Firebase Auth 要求设置 Authorization
header。
我的尝试是否可行?
如有任何建议,我们将不胜感激。也许使用 gapi 客户端无法调用经过 Firebase 身份验证的端点。
这是我的 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)
});
}
我为此苦苦挣扎了一段时间,终于成功了。我找到了两个选项:
选项 1) 如果您想使用 gapi.client 库:
有一个方法叫做 gapi.client.setToken(tokenObject) - documentation
但是,它似乎是新的(2017 年 7 月)并且可用的文档或示例很少。我做了以下工作(这是在 angularJS 中使用 angular-fire 但我希望你明白我在做什么,基本上忽略“$scope”)
// 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。特别是确保再次创建 lib 文件夹。
即使在 SDK 更新之后,我注意到如果并且一旦您从 v1 迁移到 v2,无论是否更新,"lib" 文件夹都不会更新。
还是不行?
github page 修复了早期版本后端的问题——后端不接受 firebase 令牌,需要被破解。如果你想应用那里描述的更改并且你正在使用最新的 "lib" 文件夹(写于 2017 年 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 友好且安全。
我在 Python 中编写了几个 Google 云端点,并且 followed the directions 要求对它们的调用来自使用 Firebase 进行身份验证的用户。我需要使用 JavaScript 从 Web 应用程序调用我的端点,但我似乎无法进行身份验证。
我想使用 Google APIs client (gapi),它具有从提供的发现文档动态生成客户端库的额外好处。当我尝试使用 gapi 客户端时,我可以很好地调用我的 API,但我得到一个 HTTP 401 作为响应,以及我的 python 来源 [=36] 的 HTTP 未授权消息=].
Google 关于这个主题的文档相当稀少。我从 one tutorial on the subject 了解到可以使用标准 Ajax 调用,但我没有看到任何关于如何从 Gapi 调用经过 Firebase 身份验证的端点的文档。我目前担心的是,gapi 客户端可能(尚未)设置为允许使用发现文档,并且还允许根据 Firebase Auth 要求设置 Authorization
header。
我的尝试是否可行?
如有任何建议,我们将不胜感激。也许使用 gapi 客户端无法调用经过 Firebase 身份验证的端点。
这是我的 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)
});
}
我为此苦苦挣扎了一段时间,终于成功了。我找到了两个选项:
选项 1) 如果您想使用 gapi.client 库: 有一个方法叫做 gapi.client.setToken(tokenObject) - documentation
但是,它似乎是新的(2017 年 7 月)并且可用的文档或示例很少。我做了以下工作(这是在 angularJS 中使用 angular-fire 但我希望你明白我在做什么,基本上忽略“$scope”)
// 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。特别是确保再次创建 lib 文件夹。 即使在 SDK 更新之后,我注意到如果并且一旦您从 v1 迁移到 v2,无论是否更新,"lib" 文件夹都不会更新。
还是不行? github page 修复了早期版本后端的问题——后端不接受 firebase 令牌,需要被破解。如果你想应用那里描述的更改并且你正在使用最新的 "lib" 文件夹(写于 2017 年 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 友好且安全。