Android Studio - 从GetIdToken获取Firebase令牌

我在 Swift 中做了以下工作:

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's really long)
   //..do stuff with token
}

我现在正试图为Android做等效的事情。firebase文档涉及该主题,但没有广泛解释获取令牌。我尝试了以下方法:

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

但是,当我尝试在后端服务器上单独进行身份验证时,这给我带来了以下错误:

错误:解码 Firebase ID 令牌失败。确保传递了表示 ID 令牌的整个字符串 JWT。有关如何检索 ID 令牌的详细信息,请参阅 https://firebase.google.com/docs/auth/admin/verify-id-tokens。at FirebaseAuthError.Error (native) at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28) at new FirebaseAuthError (/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23) at FirebaseTokenGenator.verifyIdToken (/user_code/node_modules/firebase-admin/lib/auth/token-generator.js:155:35) at Auth.verifyIdToken (/user_code/node_modules/firebase-admin/lib/auth/auth.js:104:37)admin.database.ref.child.child.child.child.child.child.orderByChild.once.then.snapshot (/user_code/index.js:1430:22) at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我相信这是因为需要有一个onSuccessListener,但我不确定,也没有成功实现它,如下所示:

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

答案 1

您的第二种方法很接近,您只需要使用Firebase存储上传图像,而不是因为使用Firebase存储上传图像。<GetTokenResult><UploadTask.TaskSnapshot>

试试这个:

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

答案 2

您可以获取用户令牌,如下所示代码

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

推荐