[英]Firebase on mobile how to authenticate call to cloud function?
I am using Firebase with my React Native app, and I only have anonymous login enabled.我将 Firebase 与我的 React Native 应用程序一起使用,并且我只启用了匿名登录。
I am trying to call a Cloud Function
from the app, but not using the firebase
SDK.我正在尝试从应用程序调用
Cloud Function
,但不使用firebase
SDK。 Instead I want to call it with axios
.相反,我想用
axios
来调用它。 When I pass the token grabbed from my firebase
user as a Bearer token, I get back a 401 Unauthorized.当我将从我的
firebase
用户获取的令牌作为不记名令牌传递时,我得到了 401 Unauthorized。
However, the request goes through if I copy the token I get from the CLI with gcloud auth print-identity-token
.但是,如果我使用
gcloud auth print-identity-token
复制从 CLI 获得的令牌,请求就会通过。
How can I grab/setup a valid token that I could pass to the Cloud Function?如何获取/设置可以传递给云 Function 的有效令牌? Any relevant doc would be appreciated.
任何相关的文档将不胜感激。
Im trying to call the function using a onRequest
:我试图使用
onRequest
调用 function :
await axios.post(CLOUD_FUNC_URL, payload, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${TOKEN}`,
},
});
I am either grabbing the token from gcloud CLI
(works) but the one from await auth().currentUser?.getIdToken();
我要么从
gcloud CLI
获取令牌(有效),要么从await auth().currentUser?.getIdToken();
does not.才不是。
Your function must be private (requires authentication) by default.默认情况下,您的 function 必须是私有的(需要身份验证)。 It works with your token (
gcloud auth print-identity-token
) because you may be the owner of the project.它适用于您的令牌 (
gcloud auth print-identity-token
),因为您可能是该项目的所有者。 You can go to Cloud Functions console and add a principal allUsers
with Cloud Functions Invoker
role so it can be invoked by everyone.您可以将 go 到Cloud Functions 控制台并添加一个具有
Cloud Functions Invoker
角色的主体allUsers
,以便每个人都可以调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.