我得到了错误
gettingdocuments.com.google.firebase.firestore.FirebaseFirestoreException:
PERMISSION_DENIED:缺少或权限不足。
对于下面关于else语句的代码
db.collection("users")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
s(document.getId() + " => " + document.getData());
}
} else {
s("Error getting documents."+ task.getException());
}
}
});
时间限制可能已过
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This rule allows anyone on the internet to view, edit, and delete
// all data in your Firestore database. It is useful for getting
// started, but it is configured to expire after 30 days because it
// leaves your app open to attackers. At that time, all client
// requests to your Firestore database will be denied.
//
// Make sure to write security rules for your app before that time, or else
// your app will lose access to your Firestore database
match /{document=**} {
allow read, write: if request.time < timestamp.date(2020,7, 1);
}
}
}
这一行更改了日期:
allow read, write: if request.time < timestamp.date(2020,7, 1);
另一个原因是AppCheck。我在一个新项目上启用了它(创建~ 2022年5月),但还没有完成集成步骤,导致“缺少或权限不足”错误。
要解决此问题,首先完成Firebase中AppCheck部分中列出的AppCheck设置步骤。我在我的web应用程序中使用了ReCAPTCHA提供者,你需要复制ReCAPTCHA公钥来在你的代码库中使用。
接下来,在你初始化firebase应用的任何地方添加AppCheck初始化代码。我的在React中是这样的:
import { initializeAppCheck, ReCaptchaV3Provider } from 'firebase/app-check';
// ......
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
self['FIREBASE_APPCHECK_DEBUG_TOKEN'] = true;
// Pass your reCAPTCHA v3 site key (public key) to activate(). Make sure this
// key is the counterpart to the secret key you set in the Firebase console.
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider('PASTE KEY HERE'),
// Optional argument. If true, the SDK automatically refreshes App Check
// tokens as needed.
isTokenAutoRefreshEnabled: true,
});
注意FIREBASE_APPCHECK_DEBUG_TOKEN行在浏览器控制台中打印了一个调试令牌,你需要将它复制回AppCheck下的Firebase控制台中以完成设置,之后你可以注释/删除该行。
这解决了我的问题。
进一步的信息:
https://firebase.google.com/docs/app-check/web/recaptcha-provider
https://firebase.google.com/docs/app-check/web/debug-provider?authuser=0&hl=en