猿问

在 Java 中解码 Firebase 令牌

我正在尝试在后端验证/解码令牌,然后从解码的令牌中检索用户 UID。令牌由客户端生成为字符串。后端成功地将数据检索/设置到 Firebase,这让我相信 Firebase 正在正确初始化,所以这不是问题。但是,在运行时,似乎存在导致崩溃的错误。


代码:


public static void main(String[] args) throws IOException, ExecutionException, InterruptedException, FirebaseAuthException {

        File credentialsFile = getFile();


        FileInputStream serviceAccount = new FileInputStream(credentialsFile);


        FirebaseOptions options = new FirebaseOptions.Builder()

                .setCredentials(GoogleCredentials.fromStream(serviceAccount))

                .setDatabaseUrl("***")

                .build();


        FirebaseApp.initializeApp(options);


        String idToken = "***";


        //LINE 43 IS BELOW

        FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdToken(idToken);

        String uid = decodedToken.getUid();


        System.out.println("UID: " + uid);

    }

错误:


Exception in thread "main" java.lang.IllegalArgumentException

    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:108)

    at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)

    at com.google.api.client.json.webtoken.JsonWebSignature$Parser.parse(JsonWebSignature.java:599)

    at com.google.firebase.auth.FirebaseToken.parse(FirebaseToken.java:44)

    at com.google.firebase.auth.FirebaseAuth$4.execute(FirebaseAuth.java:484)

    at com.google.firebase.auth.FirebaseAuth$4.execute(FirebaseAuth.java:477)

    at com.google.firebase.internal.CallableOperation.call(CallableOperation.java:36)

    at com.google.firebase.auth.FirebaseAuth.verifyIdToken(FirebaseAuth.java:441)

    at com.google.firebase.auth.FirebaseAuth.verifyIdToken(FirebaseAuth.java:415)

    at App.main(App.java:43)


FAILURE: Build failed with an exception.


一只斗牛犬
浏览 108回答 1
1回答

九州编程

解决方案:正如hiranya-jayathilaka所指出的,问题在于从客户端生成令牌的方式。这就是我生成令牌的方式:String idToken = FirebaseInstanceId.getInstance().getToken();这是根据文档生成的方式:FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mUser.getIdToken(true)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onComplete(@NonNull Task<GetTokenResult> task) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String idToken = task.getResult().getToken();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Send token to your backend via HTTPS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Handle error -> task.getException();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });
随时随地看视频慕课网APP

相关分类

Java
我要回答