Flutter 中 facebookAuthCredential.idToken 为空

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

我成功地将 facebook 登录集成到 android 和 ios 中,我还在登录时获得了 facebook 访问令牌,但令牌 id 为空。下面是代码

  Future<UserCredential> signInWithFacebook() async {
    // Trigger the sign-in flow
    final LoginResult loginResult = await FacebookAuth.instance.login();

    // Create a credential from the access token
    final OAuthCredential facebookAuthCredential =
        FacebookAuthProvider.credential(loginResult.accessToken!.token);
    print(facebookAuthCredential.idToken); //Here getting null

    // Once signed in, return the UserCredential
    return FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);
  }
firebase flutter dart flutter-facebook-login
2个回答
3
投票

此时,通过 Facebook 身份验证流程,您不会获得

idToken
,只会获得
accessToken
。使用以下代码框架来管理 Facebook 登录并使用断点评估特定行的结果:

Future<UserCredential> signInWithFacebook() async {
  final LoginResult loginResult = await FacebookAuth.instance.login();

  if (loginResult.status == LoginStatus.success) {
    final AccessToken accessToken = loginResult.accessToken!;
    final OAuthCredential credential =
        FacebookAuthProvider.credential(accessToken.token);
    try {
      return await FirebaseAuth.instance.signInWithCredential(credential);
    } on FirebaseAuthException catch (e) {
      // manage Firebase authentication exceptions
    } catch (e) {
      // manage other exceptions
    }
  } else {
      // login was not successful, for example user cancelled the process
  }
}

然后你可以用

await
调用这个函数,一旦future完成,你就可以访问用户数据了:

final userCredential = await signInWithFacebook();
if (userCredential != null) {
    // here you will have your Firebase user in:
    // userCredential.user
    final idToken = userCredential.user!.getIdToken();
}

0
投票

if (accessToken != null) { // 使用 accessToken 实例中的令牌 最终 OAuthCredential facebookAuthCredential = FacebookAuthProvider.credential(accessToken.tokenString);

      UserCredential userCredential =
      await FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);
© www.soinside.com 2019 - 2024. All rights reserved.