安卓Firebase在尝试访问存储时抛出身份验证错误。

huangapple go评论71阅读模式
英文:

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!=nullallow 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: {  &quot;error&quot;: {    &quot;code&quot;: 403,    &quot;message&quot;: 
&quot;Permission denied. Could not perform this operation&quot;  }}

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(&quot;gs://XXXXXX-XXXX.appspot.com/Books/XXXX&quot;).listAll().addOnSuccessListener(new OnSuccessListener&lt;ListResult&gt;() {
            @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, &quot;File download failed!&quot;, Toast.LENGTH_LONG).show();
                                System.out.println(&quot;ERROR: &quot;+ e.toString());
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    //System.out.println(&quot;IIIIII: &quot;+sref.getName());
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(tabtwoactivity.this, &quot;Fetching Directory XXXXX Failed&quot;, Toast.LENGTH_LONG).show();
            }
        });

I've tried using fstore.child(&quot;XXXX&quot;).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;
    }
  }
}

huangapple
  • 本文由 发表于 2020年9月27日 19:59:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64088219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定