gpt4 book ai didi

java - Android Studio - 从 GetIdToken 获取 Firebase token

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:54:45 26 4
gpt4 key购买 nike

我在 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 documentation触及主题但没有广泛解释获取 token 。我尝试了以下方法:

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

但是,当我尝试在我的后端服务器上单独验证它时,这给了我以下错误:

Error: Decoding Firebase ID token failed. Make sure you passed the entire string JWT which represents an ID token. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token. 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 FirebaseTokenGenerator.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) at 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);
}
});

最佳答案

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

试试这个:

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

关于java - Android Studio - 从 GetIdToken 获取 Firebase token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45427735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com