英文:
Android firebase throws authentication error while trying to access storage
问题
EDIT: 我刚刚在清理项目并清除缓存后重新启动了Android Studio。现在我得到了以下错误 -
E/StorageException: { "error": { "code": 403, "message": "权限被拒绝。无法执行此操作" }}
我得到了无限打印以下错误信息。
2020-09-27 17:00:32.931 11755-11863/com.example.XXXXXX E/StorageUtil: 获取令牌时出错java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: 请在尝试获取令牌之前登录。
在我的应用中,我已经正确地创建了用户,并且FirebaseAuth.getInstance().getCurrentUser();不会返回null。但如果它确实返回null,我会通过成功登录或创建另一个用户来处理它。在不到半天的时间内,我已经创建了160多个用户。我不认为我为了测试而运行了那么多次应用程序。在身份验证设置中启用了“使用电子邮件登录”和“匿名登录”选项。
问题在于当我尝试访问存储时出现。我得到了上述提到的身份验证令牌错误。文件夹和文件是存在的,以下是我尝试下载文件的方式 -
fstore.child("gs://XXXXXX-XXXX.appspot.com/Books/XXXX").listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
@Override
public void onSuccess(ListResult listResult) {
for(StorageReference sref : listResult.getItems())
{
tempFilename = sref.getName();
File tmpFile = new File(tempSubDir, tempFilename);
try {
tmpFile.createNewFile();
sref.getFile(tmpFile).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(tabtwoactivity.this, "文件下载失败!", Toast.LENGTH_LONG).show();
System.out.println("错误:" + e.toString());
}
});
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println("IIIIII:" +sref.getName());
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(tabtwoactivity.this, "获取目录XXXXX失败", Toast.LENGTH_LONG).show();
}
});
我尝试使用fstore.child("XXXX").listAll(),但是那给我带来了一系列不同的错误,主要集中在未找到文件夹。我的存储中有文件夹和文件。我甚至尝试过更改存储规则 - allow read, write;, allow read, write: if request.auth!=null, allow read, write: if request.auth==null。但是都没有起作用。
我做错了什么?
英文:
EDIT: I just restarted Android Studio after cleaning the project and invalidating cache. Now I get this error -
E/StorageException: { "error": { "code": 403, "message":
"Permission denied. Could not perform this operation" }}
I'm getting the following error that prints infinitely.
> 2020-09-27 17:00:32.931 11755-11863/com.example.XXXXXX E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
In my app I've created the user properly, and FirebaseAuth.getInstance().getCurrentUser(); does NOT return null. But if it does, I handle it by logging them in successfully or creating another user. My app now has 160+ users created, in less than half a day. I don't think I've even run the app that many times for testing. 'Sign in with email', and 'anonymous sign in' options are enabled in auth settings.
The problem starts when I try to access the storage. I get the above mentioned auth token error. The folder and file exist and this is how I'm trying to download the files -
fstore.child("gs://XXXXXX-XXXX.appspot.com/Books/XXXX").listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
@Override
public void onSuccess(ListResult listResult) {
for(StorageReference sref : listResult.getItems())
{
tempFilename = sref.getName();
File tmpFile = new File(tempSubDir, tempFilename);
try {
tmpFile.createNewFile();
sref.getFile(tmpFile).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(tabtwoactivity.this, "File download failed!", Toast.LENGTH_LONG).show();
System.out.println("ERROR: "+ e.toString());
}
});
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println("IIIIII: "+sref.getName());
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(tabtwoactivity.this, "Fetching Directory XXXXX Failed", Toast.LENGTH_LONG).show();
}
});
I've tried using fstore.child("XXXX").listAll() but that gave me an avalanche of different errors, mainly focused around Folder not found. My storage has the folder and files. I've even played around with the storage rules - allow read, write;, allow read, write: if request.auth!=null, allow read, write: if request.auth==null. But nothing worked.
What am I doing wrong?
答案1
得分: 1
以下是翻译好的内容:
将此内容粘贴到您的 google.json 文件中:
{
"rules": {
"service": {
"firebase.storage": {
// 通配符 {bucket} 表示我们匹配所有存储桶中的文件
"match": "/b/{bucket}/o",
// 匹配文件名
"rules": {
"match": "/filename",
"allow": {
"read": "true",
"write": "true"
}
}
}
}
}
}
英文:
Paste this in your google.json:
service firebase.storage {
// The {bucket} wildcard indicates we match files in all storage buckets
match /b/{bucket}/o {
// Match filename
match /filename {
allow read: if true;
allow write: if true;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论