【问题标题】:Flutter: FirebaseAuth token error: The custom token format is incorrect. Please check the documentationFlutter:FirebaseAuth 令牌错误:自定义令牌格式不正确。请检查文档
【发布时间】:2019-12-21 11:40:18
【问题描述】:

我正在 Flutter 中使用 Line App Login,我可以在 Line API 中获取用户令牌,但我不知道下一步该做什么。

现在我正在尝试在 FirebaseAuth.instance.signInWithCustomToken(token:customToken) 中使用它,但它不起作用。错误日志显示“PlatformException(ERROR_INVALID_CUSTOM_TOKEN,自定义令牌格式不正确。请查看文档。[无效的断言格式。],null)”是否有解决此问题的方法?

lineSignIn() async {
    _flutterLineLogin.startLogin(_onLoginSuccess, _onLoginError);
  }

  void _onLoginSuccess(dynamic value) {
  FirebaseAuth.instance.signInWithCustomToken(token:value['accessToken'].toString()).then((onValue) {
      if (onValue != null) {
 ///this should be success
      }
    }).catchError((onError) {
      print(onError.toString());
    });
  }

  void _onLoginError(Object error) {
    debugPrint("PlatformException: ${error}");
  }

我只需要在 firebase 中验证令牌。提前谢谢大家!

【问题讨论】:

  • 这里也一样。你找到解决办法了吗?

标签: android ios firebase flutter google-cloud-firestore


【解决方案1】:

使用 FirebaseAuth.instance.signInWithCredential 代替 FirebaseAuth.instance.signInWithCustomToken

我也在使用相同的源并尝试使用Firebase 实例获取AuthResult,但由于低于FirebaseAuth 错误而失败。

PlatformException(ERROR_INVALID_CUSTOM_TOKEN, 自定义令牌格式 是不正确的。请检查文档。 [无效的断言 格式。需要 3 个点分隔段。 ],空)

例如,见下文Facebook登录源:

_loginFacebookUser() async {
    try {

      // getting token using email or public profile
      _facebookLogin.logIn(["email", "public_profile"]).then((FacebookLoginResult fbResult) {

        if (fbResult != null) {

          // if facebook login result is loggedIn then
          if (fbResult.status == FacebookLoginStatus.loggedIn) {

            String facebookToken = fbResult.accessToken.token;

            print("facebookToken is: ${facebookToken}");

            AuthCredential credential =  FacebookAuthProvider.getCredential(accessToken: facebookToken);

            // generating firebase authResult for facebook
//            FirebaseAuth.instance.signInWithCustomToken(token: facebookToken).then((AuthResult authResult) {
//
//              AdditionalUserInfo userInfo = authResult.additionalUserInfo;
//              print("facebook userName is: ${userInfo.username}");
//              print("facebook user profile is: ${userInfo.profile}");
//
//            }).catchError((error) {
//              print("error occurred while firebase tokenizing: $error");
//            });

            FirebaseAuth.instance.signInWithCredential(credential).then((AuthResult authResult) {

              AdditionalUserInfo userInfo = authResult.additionalUserInfo;
              print("facebook userName is: ${userInfo.username}");
              print("facebook user profile is: ${userInfo.profile}");

            }).catchError((error) {
              print("error occurred while firebase tokenizing: $error");
            });

          }
        } else {
          print("facebook login result is null");
        }

      }).catchError((error) {
        print("facebook login error: $error");
      });

      setState(() {});

    } catch (error) {
      print("error occurred while loggin: $error");
  }
}

希望对某人有所帮助。

【讨论】:

  • 我不知道为什么有人在这个答案上减去标记,这工作正常。
  • 应该标记为正确答案!非常感谢
  • 人们可能正在寻找使用自定义令牌的示例,因为这就是标题所暗示的。但是,您显示 signInWithCredential。
  • 这个有什么解决办法吗?
【解决方案2】:

同样,对我来说......

就我而言,我使用 Webview 与我的 Firebase API(这是一个全局的 Instagram API 调用,并创建了一个 Firebase JWT ) 当我输入我在 cookie 中传递给函数 FirebaseAuth.instance.signInWithCustomToken() 的令牌时,我得到了同样的错误 PlatformException(ERROR_INVALID_CUSTOM_TOKEN, The custom token format is incorrect. Please check the documentation.

我将我的 JWT 传递给 jwt.io,这似乎是正确的......但不适用于 Firebase:Decoded JavaScript Web Token IMAGE

此错误与 Firebase 或 Flutter 有关吗?

如果有人知道错误在哪里... 请告诉我。

祝你有美好的一天。

【讨论】:

  • 任何。关于这个的更新,我被这个困住了?
猜你喜欢
  • 2018-01-06
  • 2018-10-16
  • 1970-01-01
  • 2017-02-08
  • 2016-12-04
  • 2017-02-16
  • 2018-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多