解码在Java中火力地堡令牌

问题描述 投票:0回答:1

我想验证/在我的后端解码令牌,然后检索从解码令牌用户的UID。令牌由客户机,作为一个字符串生成。后端成功地检索/数据集以火力地堡这使我相信,火力地堡被正确初始化,这样就不会成为问题。但是在运行时,似乎是导致崩溃的错误。

码:

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.
java firebase firebase-authentication firebase-admin
1个回答
1
投票

解:

问题是与正在由客户端生成的令牌,通过hiranya-jayathilaka指出了道路。这就是我正在生成令牌:

String idToken = FirebaseInstanceId.getInstance().getToken();

这是怎么回事应根据docs产生:

FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser();
            mUser.getIdToken(true)
                    .addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
                        public void onComplete(@NonNull Task<GetTokenResult> task) {
                            if (task.isSuccessful()) {
                                String idToken = task.getResult().getToken();
                                // Send token to your backend via HTTPS
                                // ...
                            } else {
                                // Handle error -> task.getException();
                            }
                        }
                    });
© www.soinside.com 2019 - 2024. All rights reserved.