Firebase admin sdk:IllegalArgumentException,Preconditions.checkArgument

Firebase admin sdk: IllegalArgumentException, Preconditions.checkArgument

我收到以下错误:

java.lang.IllegalArgumentException
    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111)
    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.execute(FirebaseAuth.java:456)
    at com.google.firebase.auth.FirebaseAuth.execute(FirebaseAuth.java:449)
    at com.google.firebase.internal.CallableOperation.call(CallableOperation.java:36)
    at com.google.firebase.auth.FirebaseAuth.verifyIdToken(FirebaseAuth.java:413)
    at d.d.pamper.test.ApplicationTest.testFire2(ApplicationTest.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我意识到堆栈不是很有用。我已经在 Admin SDK 中进行了调试。它正确读取了我的服务 json,FirebaseAuth.getInstance() 不为空,我可以看到我的项目 ID 设置正确。

这就是我的初始化方式:

if(FirebaseApp.getApps().size() > 0) {
            System.out.println("initialized already ");
            return;
        }

        FirebaseOptions options = null;
        try {
            System.out.println("try initializing");
            options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(gservicesConfig.getInputStream()))
                    .setDatabaseUrl("https://pamperanywhere.firebaseio.com")
                    .build();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FirebaseApp.initializeApp(options);
        return;

这就是我测试令牌的方式:

@Test
public void testFire2() {
    String idToken = "REFRESH TOKEN FROM FIREBASE";
    if(!StringUtils.isEmpty(idToken)) {
        FirebaseToken firebaseToken = null;
        if(FirebaseAuth.getInstance() == null) {
            System.out.println("FirebaseAuth.getInstance() == null");
        }
        try {
            firebaseToken = FirebaseAuth.getInstance().verifyIdToken(idToken, true);
        } catch (IllegalArgumentException e) {
            System.out.println("exception+IllegalArgumentException");
            e.printStackTrace();
        } catch (FirebaseAuthException e) {
            System.out.println("exception+FirebaseAuthException");
            e.printStackTrace();
        }
        if (firebaseToken == null) {
            System.out.println("firebaseToken == null");
        }
    }
}

我不确定它为什么会抛出 IllegalArgumentException。我能够很好地登录到应用程序(前端),我看到了我的刷新令牌(前端),但在管理员端它给了我这个问题。我确定我正在传递正确的令牌,因为我可以登录并查看令牌(前端)。

Maven 是:

    <dependency>
        <groupId>com.google.firebase</groupId>
        <artifactId>firebase-admin</artifactId>
        <version>6.2.0</version>
    </dependency>

感谢您的帮助。

ID 令牌应该是具有 3 个段(以点分隔)的有效 JWT。刷新令牌当然不是那样。因此,底层令牌解析器因上述断言错误而失败。这是来自 Google API 客户端的相关代码。基本上它在给定的标记字符串中找不到任何点。

public JsonWebSignature parse(String tokenString) throws IOException {
      // split on the dots
      int firstDot = tokenString.indexOf('.');
      Preconditions.checkArgument(firstDot != -1);
      ...

有一个听众:

firebase.auth().onIdTokenChanged((user: firebase.User) => {
      console.log("onIdTokenChanged");
      if(user && user.uid) {
        user.getIdToken(false)
        .then((data: any) => {
          console.log("data: ", data);
          this.idToken = data;
        });
      }      
    });

它每隔一小时调用一次。我以为我必须自己实现这个逻辑! https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onIdTokenChanged

来自官方;以上来自 Ionic2/Angular2,我在 dataServiceProvider 的构造函数中有它。

firebase.auth().onIdTokenChanged(function(user) {
  if (user) {
    // User is signed in or token was refreshed.
  }
});