简体   繁体   English

如何在 React 中使用 Firebase App Check。 403错误

[英]How to use Firebase App Check in React. 403 error

I want to use Firebase App Check in Gatsby Project.I have completed the App registration in the Firebase console.我想在 Gatsby 项目中使用 Firebase App Check。我已经在 Firebase 控制台中完成了 App 注册。

在此处输入图片说明

In my project:在我的项目中:

import { initializeApp } from "firebase/app";
import { initializeAppCheck, ReCaptchaV3Provider, getToken } from "firebase/app-check";

const app = initializeApp({
  // My firebase configuration object
});

const appCheck = initializeAppCheck(app, {
  provider: new ReCaptchaV3Provider('MY_RECAPTCHA_PUBLIC_KEY'),
  isTokenAutoRefreshEnabled: true
});

getToken(appCheck)
  .then(() => {
    console.log('success')
  })
  .catch((error) => {
    console.log(error.message)
  })

But, it not working and got the error:但是,它不起作用并出现错误:

@firebase/app-check: FirebaseError: AppCheck: Fetch server returned an HTTP error status. @firebase/app-check: FirebaseError: AppCheck: Fetch 服务器返回 HTTP 错误状态。 HTTP status: 403. (appCheck/fetch-status-error). HTTP 状态:403。(appCheck/fetch-status-error)。

So, can anyone help me check where the problem is?那么,谁能帮我检查一下问题出在哪里? How to solve?怎么解决?

Thanks!谢谢!

Go to your Firebase Project in Google Cloud Console: https://console.cloud.google.com/apis/credentials在 Google Cloud Console 中转到您的 Firebase 项目: https : //console.cloud.google.com/apis/credentials

Click on the name of your Firebase API Key under "API Keys".在“API Keys”下点击您的 Firebase API Key 的名称。

"API restrictions" “API 限制”

If you are restricting use of your API key, make sure you include:如果您限制使用 API 密钥,请确保您包括:

Selected APIs : Firebase App Check API选定的 API :Firebase 应用检查 API

Adding onto @jprio's answer:添加到@jprio 的答案中:

If you are using an unrestricted API key and using ReCaptchaEnterprise, you must make sure that you correctly have the appropriate domains set for your Recaptcha Enterprise Site Key.如果您使用不受限制的API 密钥并使用 ReCaptchaEnterprise,则必须确保为您的 Recaptcha Enterprise 站点密钥正确设置了适当的域。

  1. In the Google Cloud console, go to Security, then reCAPTCHA Enterprise: https://console.cloud.google.com/security/recaptcha在 Google Cloud 控制台中,转到安全性,然后是 reCAPTCHA Enterprise: https : //console.cloud.google.com/security/recaptcha
  2. Click on the three dots and press "Edit Key" on your generated key单击三个点,然后在生成的密钥上按“编辑密钥” 编辑企业站点密钥
  3. Under Domain List , enter in the domain of your website, no need to add any HTTP/HTTPS/WWW prefixes, or port numbers.Domain List 下,输入您网站的域,无需添加任何 HTTP/HTTPS/WWW 前缀或端口号。
  4. If you are developing locally, be sure to add localhost/127.0.0.1.如果是本地开发,一定要添加localhost/127.0.0.1。 This is what solved the issue for me.这就是为我解决问题的原因。 Save changes.保存更改。 DO NOT FORGET to remove these before you deploy to your website在部署到您的网站之前不要忘记删除这些在此处输入图片说明
  5. Hit refresh on your client after about 5 minutes, allowing for the changes to propagate.大约 5 分钟后在您的客户端上刷新,允许更改传播。 Be sure you are using ReCaptchaEnterpriseProvider in your client if you are using ReCaptcha Enterprise.如果您使用的是 ReCaptcha Enterprise,请确保您在客户端中使用 ReCaptchaEnterpriseProvider。

I had a similar issue.我有一个类似的问题。 For me, the error got resolved when I used the correct appId in the firebase config.对我来说,当我在 firebase 配置中使用正确的 appId 时,错误得到了解决。 It turns out that I had set up two apps on firebase and I downloaded the wrong json file which had a different appId.事实证明,我在 firebase 上设置了两个应用程序,并且下载了具有不同 appId 的错误 json 文件。

According to firebase support, appIds are unique per app.根据 firebase 支持,每个应用的 appId 都是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM