Android Studio - Get Firebase token from GetIdToken
Your second approach is close, you just need to use <GetTokenResult>
instead of <UploadTask.TaskSnapshot>
as that is for uploading images using Firebase Storage.
Try this:
user.getIdToken(true).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
@Override
public void onSuccess(GetTokenResult result) {
String idToken = result.getToken();
//Do whatever
Log.d(TAG, "GetTokenResult result = " + idToken);
}
});
You can get the user token like below code
FirebaseAuth mAuth = FirebaseAuth.getInstance();
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
information
FirebaseUser user = Objects.requireNonNull(task.getResult()).getUser();
assert user != null;
user.getIdToken(true).addOnSuccessListener(result -> {
String idToken = result.getToken();
//Do whatever
Log.d(TAG, "GetTokenResult result = " + idToken);
});
} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
Toast.makeText(getApplicationContext(), "Your code is not correct!", Toast.LENGTH_SHORT).show();
code.");
}
}
}
});
}