我成功地将 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);
}
此时,通过 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();
}
if (accessToken != null) { // 使用 accessToken 实例中的令牌 最终 OAuthCredential facebookAuthCredential = FacebookAuthProvider.credential(accessToken.tokenString);
UserCredential userCredential =
await FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);