gpt4 book ai didi

javascript - 如何从 Javascript 调用经过 Firebase 身份验证的云端点?

转载 作者:行者123 更新时间:2023-11-28 04:54:04 25 4
gpt4 key购买 nike

我已经用 Python 编写了多个 Google Cloud Endpoints,并且有 followed the directions要求对它们的调用来自使用 Firebase 进行身份验证的用户。我需要使用 JavaScript 从 Web 应用程序调用我的端点,但我似乎无法进行身份验证。

我想使用 Google APIs client (gapi) 具有从提供的发现文档动态生成客户端库的额外好处。当我尝试使用gapi客户端时,我可以很好地调用我的API,但我得到一个HTTP 401作为响应,以及我的python源返回的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
}
});

如果您的后端仍然不接受 token 并且您已从端点 v1 迁移到 v2,这可能会有所帮助 migrating again as described here 。特别是。确保再次创建 lib 文件夹。即使在 SDK 更新之后,我注意到,如果您从 v1 迁移到 v2,“lib”文件夹永远不会更新,无论它是否已更新。

仍然无法工作?这个github page修复了早期版本后端的问题 - 后端不接受 firebase token ,需要进行黑客攻击。如果您想应用此处所述的更改,并且您正在使用最新的“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.

并在检查签名之前解析 token 。然而,从该文件的注释可以推断,Google 计划将整个逻辑放在其他地方 - 希望 firebase 友好且安全。

关于javascript - 如何从 Javascript 调用经过 Firebase 身份验证的云端点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42687987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com