Android Studio-从GetIdToken获取Firebase令牌 [英] Android Studio - Get Firebase token from GetIdToken

查看:346
本文介绍了Android Studio-从GetIdToken获取Firebase令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Swift中完成了以下操作:

I have done the following in 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文档涉及该主题,但没有解释广泛获得令牌.我尝试了以下方法:

I am now trying to do the equivalent for Android. The firebase documentation touches on the topic but does not explain getting the token extensively. I have tried the following:

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

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

However, this gives me the following error when I attempt to authenticate this alone on my backend server:

错误:解码Firebase ID令牌失败.确保您通过了 代表ID令牌的整个字符串JWT.看 https://firebase.google.com/docs/auth/admin/verify -id-tokens 有关如何获取ID令牌的详细信息. 在FirebaseAuthError.Error(本机) 在FirebaseAuthError.FirebaseError [作为构造函数](/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28) 在新的FirebaseAuthError(/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23) 在FirebaseTokenGenerator.verifyIdToken(/user_code/node_modules/firebase-admin/lib/auth/token-generator.js:155:35) 在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) 在process._tickDomainCallback(internal/process/next_tick.js:135:7)

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,但不确定,也没有成功实现它,如下所示:

I believe this is because there needs to be an onSuccessListener but am not sure, nor have had success implementing it as follows:

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 Storage上传图像的方法.

Your second approach is close, you just need to use <GetTokenResult> instead of <UploadTask.TaskSnapshot> as that is for uploading images using Firebase Storage.

尝试一下:

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

这篇关于Android Studio-从GetIdToken获取Firebase令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆