简体   繁体   English

Firebase 在手机上如何验证对云 function 的调用?

[英]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.

相关问题 如何从 React stream 聊天应用程序调用 Firebase 云 function - How to call Firebase cloud function from React stream chat app 如何从统一中调用 firebase 示例云 function? - how to call a firebase sample cloud function from unity? Firebase Cloud Functions - 如何调用另一个函数并返回对象 - Firebase Cloud Functions - How to call another function and return object 如何从 Firebase 云调用异步函数 Function - How to call async functions from a Firebase Cloud Function Firebase:重新验证云中的当前用户 Function - Firebase: Re-authenticate the current user inside a Cloud Function 如何为 HTTP 触发器验证谷歌云 function? - How to authenticate google cloud function for HTTP trigger? 无法验证来自 Google Cloud Scheduler 的 HTTP function 调用 - Unable to authenticate HTTP function call from Google Cloud Scheduler 如何代表用户从云 Function 为 Firebase 调用 Google Drive API? - How to call Google Drive API from Cloud Function for Firebase on behalf of a user? Stripe Api 调用 在 Firebase 云上生成双重发票 function 调用 - Stripe Api call Generate double Invoice on Firebase cloud function call 我将如何获得 Api 密钥和 API 密钥以启用在谷歌云 firebase 中使用 Twitter 进行身份验证 - How will i get Api key and API secret for enable Authenticate Using Twitter in google cloud firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM