SonOfEl
SonOfEl

Reputation: 177

Android firebase throws authentication error while trying to access storage

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?

Upvotes: 2

Views: 334

Answers (1)

user14141430
user14141430

Reputation:

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;
    }
  }
}

Upvotes: 1

Related Questions