【发布时间】:2019-02-16 08:29:50
【问题描述】:
我想通过firebase functions:shell 作为经过身份验证的 usr 来测试 onCall 函数
我尝试了很多来自https://firebase.google.com/docs/functions/local-emulator#invoke_https_functions 的调用组合
以及下载和设置GOOGLE_APPLICATION_CREDENTIALS env 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
}
一旦部署并从我的 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,如下所示:
sendMessage.post({headers: {'Authorization': 'Bearer USERUID', 'content-type' : 'application/json'},body: JSON.stringify({data: {'messageId':'test'} }) })
我回来了:
从函数接收到响应:401,{"error":{"status":"UNAUTHENTICATED","message":"Unauthenticated"}}
您如何以经过身份验证的用户身份发送请求?
相关链接 How to test `functions.https.onCall` firebase cloud functions locally?
【问题讨论】:
标签: firebase firebase-authentication google-cloud-functions