Firebase Token Authentication error

前端 未结 5 716
予麋鹿
予麋鹿 2020-12-03 14:57

I am using firebase storage to upload files , but when I upload I am getting this error

E/StorageUtil: error getting token java.util.concurrent.ExecutionExce         


        
相关标签:
5条回答
  • 2020-12-03 15:03

    Below steps worked for me:

    1. Generated proper google-services.json from firebase with package ID

    2. In build.gradle(app)

       dependencies {
      
       compile 'com.google.firebase:firebase-storage:10.2.0'
       compile 'com.google.firebase:firebase-auth:10.2.0'
       compile 'com.google.firebase:firebase-core:10.2.0'
       compile 'com.google.firebase:firebase-database:10.2.0'
       compile 'com.firebase:firebase-client-android:2.4.0'
       }
      
    3. Make sure you add below dependencies in build.gradle(project root folder)

      dependencies {
      
      classpath 'com.android.tools.build:gradle:2.2.3'
      classpath 'com.google.gms:google-services:3.0.0'
      
      }
      
    4. While downloading file in your activity add following code:

      // Declaration reference
      private StorageReference storageRef;
      
      
      private void downloadImageFromFireBase()
      {
      showProgressDialog("Downloading image..");
      
      storageRef = storage.getReferenceFromUrl("gs://XXX.appspot.com/").child("av"+ datePassed +".jpg");
      showImageFromFireBaseDataBase();
      }
      
      private void showImageFromFireBaseDataBase()
      {
          try {
              final File localFile = File.createTempFile("images", "jpg");
              final Bitmap[] bitmap = new Bitmap[1];
                  storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                      @Override
                      public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                          Log.e("Test", "success!");
                          bitmap[0] = BitmapFactory.decodeFile(localFile.getAbsolutePath());
                          raysImage.setImageBitmap(bitmap[0]);
                      }
                  }).addOnFailureListener(new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception exception) {
                          Log.e("Test", "fail :( " + exception.getMessage());
                      }
                  });
              }catch(IOException e){
              Log.e("ImageView",e.toString());
              }
          }
      
    0 讨论(0)
  • 2020-12-03 15:05

    I think you didn't sign before uploading files. In onCreate() of launcher activity, try this code

    FirebaseAuth mAuth = FirebaseAuth.getInstance(); 
    

    Then in onStart(),

    FirebaseUser user = mAuth.getCurrentUser();
    if (user != null) {
      // do your stuff
    } else {
      signInAnonymously();
    }
    

    signInAnonymously()

    private void signInAnonymously() {
        mAuth.signInAnonymously().addOnSuccessListener(this, new  OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
                    // do your stuff
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    Log.e(TAG, "signInAnonymously:FAILURE", exception);
                }
            });
    }
    

    This may solve your problem

    0 讨论(0)
  • 2020-12-03 15:18

    Maybe you just can't "get started" to firebase. I saying that because I created a project, connect to firebase storage by Android Studio IDE, but I have to "get started" manually on console.

    0 讨论(0)
  • 2020-12-03 15:18

    It could be something as simple as internet connection, that is if all your code is correct and you have authenticated all users on firebase.

    0 讨论(0)
  • 2020-12-03 15:19

    I was facing the same issue and it was caused because by default firebase will only allow uploading files from user's that have been authenticated.

    Above the storage util exception there might be a log similar to this:

    E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.android.gms.internal.zzajb: Please sign in before trying to get a token.
    

    We had our own authentication process without using firebase so we decided to change the storage rules in firebase console.

    service firebase.storage {
      match /b/{bucket}/o {
        match /{allPaths=**} {
        allow read, write;
        }
      }
    }
    

    Note: Changing the rules will allow any user to upload files to your firebase cloud server.

    0 讨论(0)
提交回复
热议问题