问题
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.ExecutionException: com.google.android.gms.internal.zzand: Please sign in before trying to get a token.
I googled it but couldn't get answer for it! I have signed in, in firebase.
回答1:
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
回答2:
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.
回答3:
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.
回答4:
Below steps worked for me:
Generated proper google-services.json from firebase with package ID
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' }
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' }
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()); } }
回答5:
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.
来源:https://stackoverflow.com/questions/40057798/firebase-token-authentication-error