React Native Facebook logInWithReadPermissions 不工作

React Native Facebook logInWithReadPermissions not working

我正在尝试使用 react-native-fbsdk 获取用户的访问令牌。调用 logInWithReadPermissions 方法后,我不断收到一条错误消息 "undefined is not an object (evaluating 'LoginManager.logInWithReadPermissions')"

我安装了 npm 依赖并导入了它:

import { LoginManager, AccessToken } from 'react-native-fbsdk'

class 方法 loginWithFacebook 如下所示:

logInWithFacebook = () => {
  LoginManager.logInWithReadPermissions([ 'public_profile', 'user_friends' ]).then(
    (result) => {
      if(result.isCanceled) {
        alert('Login cancelled')
      } else {
        AccessToken.getCurrentAccessToken().then(({ accessToken }) => {
          console.log('Access Token : ', accessToken)
        })
      }
    },
    (error) => {
      console.error('error : ', error)
    }
  )
}

我尝试过清除缓存、将项目移动到我的文档目录、重新安装所有 npm 包……但我仍然不断收到此错误。 Click here to see the Expo debugging screen.

如有任何建议,我们将不胜感激。谢谢!

您不能在标准 Expo 项目中使用需要通过 react-native link 配置本机代码的库。为了使用上述库,您需要分离并使用 ExpoKit。以下是关于 ExpoKit 的文档:https://docs.expo.io/versions/latest/guides/detach.html

话虽如此,我们确实有一个 Facebook API 可以满足您的需求,它内置于 Expo SDK 中。 https://docs.expo.io/versions/latest/sdk/facebook.html

此外,logInWithReadPermissionsold react-native-fbsdk 版本 0.8.0 的方法。

现在你应该使用 logInWithPermissions 因为你有 react-native-fbsdk 版本 0.10.0.