Android Studio - get Firebase token from GetIdToken

In Swift, I did the following:

let currentUser = Auth.auth().currentUser
currentUser?.getTokenForcingRefresh(true) {idToken, error in
   if let error = error {
     // Handle error
     print("error (below)")
     print(error)
     return;
   }
   print("idToken = " + idToken!) // token looks like this: kpJhbGRiOiJSUzI1NiIsIntpZCI9Ijg0MjIuYzc3NTWkOWZmTjI3OBQxZTkyNTpkNWZjZjUwNzg2YTFmNGIifQ.eyJpc3MiOiJodHRwczovL3NlY3Vy... (it really long)
   //..do stuff with token
}

      

Now I'm trying to do the Android equivalent. The firebase documentation covers the topic, but does not explain how to get the token broadly. I've tried the following:

Log.d(TAG, user.getIdToken(true));

      

However, this gives me the following error when I try to authenticate just this to my server:

Error: Firebase ID decoding failed. Make sure you pass the whole JWT string that represents the ID token. See https://firebase.google.com/docs/auth/admin/verify-id-tokensfor details on how to get the ID token. in FirebaseAuthError.Error (native) in FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28) in new FirebaseAuthError (/ user_code / node_modules /lib/utils/error.js:90:23) in FirebaseTokenGenerator.verifyIdToken (/user_code/node_modules/firebase-admin/lib/auth/token-generator.js:155:35) in Auth.verifyIdToken (/ user_code / node_modules /firebase-admin/lib/auth/auth.js:104:37) at admin.database.ref.child.child.child.child.child.child.orderByChild.once.then.snapshot (/user_code/index.js : 1430: 22) to process._tickDomainCallback (internal / process / next_tick.js: 135: 7)

I believe this is because there should be an onSuccessListener, but I am not sure and have not succeeded in implementing it like this:

user.getIdToken(true).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  @Override
  public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    Log.d(TAG, "onSuccess: taskSnapshot = " + taskSnapshot);
  }
});

      

+3


source to share


1 answer


Your second approach is close, you just need to use <GetTokenResult>

instead <UploadTask.TaskSnapshot>

, as you would for uploading images using Firebase Storage.

Try the following:



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

      

+6


source







All Articles