Expo安卓应用Facebook登录报错:null is not an object (evaluating 'LoginManager.logInWithPermissions')
Expo安卓应用Facebook登录报错:null is not an object (evaluating 'LoginManager.logInWithPermissions')
问题详情
在Expo安卓应用中实现Facebook登录时,触发以下错误:
null is not an object (evaluating 'LoginManager.logInWithPermissions')
使用的登录代码:
const SignInwithFB = async () => { try { await LoginManager.logInWithPermissions(["public_profile", "email"]); const data = await AccessToken.getCurrentAccessToken(); if (!data) { return; } const facebookCredential = FacebookAuthProvider.credential(data.accessToken); const auth = getAuth(firebase); const response = await signInwithCredential(auth, facebookCredential); console.log(response); } catch (e) { console.log(e); } }
已按照react-native-fbsdk-next的文档完成安装。
解决办法
1. 换用Expo官方Facebook登录库
react-native-fbsdk-next主要适配裸React Native项目,Expo managed项目用这个库容易出现初始化失败,导致LoginManager为null。更推荐用Expo官方的expo-facebook:
- 安装依赖:
npx expo install expo-facebook
- 修改登录代码:
import * as Facebook from 'expo-facebook'; import { getAuth, signInWithCredential, FacebookAuthProvider } from 'firebase/auth'; const SignInwithFB = async () => { try { await Facebook.initializeAsync({ appId: '你的Facebook应用ID', }); const { type, token } = await Facebook.logInWithPermissionsAsync({ permissions: ['public_profile', 'email'], }); if (type === 'success') { const facebookCredential = FacebookAuthProvider.credential(token); const auth = getAuth(firebase); const response = await signInWithCredential(auth, facebookCredential); console.log(response); } } catch (e) { console.log(e); } };
2. 若坚持使用react-native-fbsdk-next(仅限Expo裸项目)
如果是Expo bare workflow项目,需检查以下配置:
- 确认
android/app/src/main/AndroidManifest.xml中已正确添加Facebook应用ID和客户端令牌 - 确保
android/build.gradle里配置了Facebook的Maven仓库 - 执行
npx pod-install(iOS端)或同步安卓项目 - 清理缓存后重启项目:
npx expo start --clear
3. 检查Facebook开发者后台配置
- 确认安卓平台的包名、签名哈希已在Facebook开发者后台正确填写
- 测试阶段可使用Facebook测试用户登录,规避未审核应用的权限限制
内容的提问来源于stack exchange,提问作者Satyam rai
相关产品推荐
相关产品推荐

