Kaz*_*anz 5 firebase firebase-authentication google-cloud-functions
我想通过firebase functions:shell作为经过身份验证的 usr测试 onCall 函数
我已经尝试了许多来自https://firebase.google.com/docs/functions/local-emulator#invoke_https_functions的调用组合
以及下载和设置GOOGLE_APPLICATION_CREDENTIALSenv var
我的函数如下所示:
exports.sendMessage = functions.https.onCall((data, context) => {
if (!context.auth) {
throw new functions.https.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
// Other logic
}
Run Code Online (Sandbox Code Playgroud)
一旦从我的 ios 应用程序部署并命中,它就可以正常工作,但我无法在 shell 中运行它。
以下命令实际上会命中该函数:
sendMessage.post({headers: {'content-type' : 'application/json'},body: JSON.stringify({data: {'messageId':'test'} }) })
并返回
RESPONSE RECEIVED FROM FUNCTION: 400, {“error”:{“status”:“FAILED_PRECONDITION”,“message”:“The function must be called while authenticated.“}}
这是正确的,但我现在想要一个经过身份验证的用户。当我尝试像文档推荐的那样添加身份验证时:
sendMessage('data', {auth:{uid:'USERUID'}}).post({headers: {'content-type' : 'application/json'},body: JSON.stringify({data: {'messageId':'test'} }) })
我最终得到 ERROR SENDING REQUEST: Error: no auth mechanism defined
如果我尝试遵循此页面https://firebase.google.com/docs/functions/callable-reference上的 Authorization 标头,如下所示:
sendMessage.post({headers: {'Authorization': 'Bearer USERUID', 'content-type' : 'application/json'},body: JSON.stringify({data: {'messageId':'test'} }) })
我回来了:
从函数收到的响应:401,{"error":{"status":"UNAUTHENTICATED","message":"Unauthenticated"}}
您如何以经过身份验证的用户身份发送请求?
根据文档,授权标头需要身份验证 ID令牌,而不是用户 ID。您必须以某种方式生成一个令牌并将其传递给该函数。然后,可调用函数将验证该令牌并通过 向函数提供用户 ID context.auth。
您似乎可以使用Firebase Auth REST API来获取这些令牌之一。或者,您可以在使用 Firebase Auth 客户端库的客户端应用程序中生成一个,记录它并复制其值,然后使用它直到过期(1 小时)。
| 归档时间: |
|
| 查看次数: |
2107 次 |
| 最近记录: |