React-native fbsdk 获取姓名和邮箱

React-native fbsdk get name and email

我正在使用 react-native-fbsdk。我想获取用户名和电子邮件以在用户注册到应用程序时显示给用户,以便为该应用程序创建配置文件,但未显示任何数据。我怎样才能得到这些数据,我怎样才能在不同的屏幕上显示它。

这是我的代码

import FBSDK from 'react-native-fbsdk'
import { LoginButton,AccessToken,GraphRequest,GraphRequestManager} from 'react-native-fbsdk';


const {
  LoginManager,
} = FBSDK;

<LoginButton
        readPermissions={['public_profile']}
          onLoginFinished={
            (error, result) => {
              if (error) {
                alert("login has error: " + result.error);
              } else if (result.isCancelled) {
                alert("login is cancelled.");
              } else {
                AccessToken.getCurrentAccessToken().then(
                  (data) => {
                    const infoRequest = new GraphRequest(
                      '/me?fields=name,picture',
                      null,
                      this._responseInfoCallback
                    );
                    // Start the graph request.
                    new GraphRequestManager().addRequest(infoRequest).start();
                  }
                )
              }
            }
          }
          onLogoutFinished={() => alert("logout.")}/>
      </View>
    );
  }
  _responseInfoCallback = (error, result) => {
    if (error) {
      alert('Error fetching data: ' + error.toString());
    } else {
      alert('Result Name: ' + result.name);
    }
  }

}

谢谢朋友

我在使用 firebase 时检索了 facebook 登录信息。

  1. 导入此文件:

    从 "react-native-firebase" 导入 Firebase;
    从 "react-native-fbsdk";

  2. 导入 { AccessToken, LoginManager }
  3. 当我点击按钮时 Firebase 登录代码工作:

    export const fbLogin = () => {

    LoginManager.logInWithReadPermissions(["public_profile", "email"])
      .then(result => {
        if (result.isCancelled) {
          console.log("Login was cancelled");
        }
        return AccessToken.getCurrentAccessToken();
      })
      .then(data => {
        const credential = firebase.auth.FacebookAuthProvider.credential(
          data.accessToken
        );
       firebase
        .auth()
        .signInWithCredential(credential)
        .then(result => {
         Toast.show({
          text: "Sucessfully",
          position: "top"
        });
       console.log("Successfully Login", result);
     })
      .catch(error => {
       console.log("Failed", error);
         });
      })
      .catch(err => {
        console.log("fail", err);
      }); 
    

    };

我检索了我的 facebook 登录数据

 console.log("Successfully Login", result);

结果 包含用户名、电子邮件、图片和..ect 凭据

如果您不想使用 firebase,

const infoRequest = new GraphRequest(
        '/me?fields=name,email,picture.type(large)',
        null,
        this._responseInfoCallback
      );
      new GraphRequestManager().addRequest(infoRequest).start();


_responseInfoCallback = (error, result) => {
    if (error) {
      alert('Error fetching data: ' + error.toString());
    } else {
      this.setState({ userName: result.name, userEmail: result.email ,userPic:result.picture.data.url});
              AsyncStorage.setItem("UserName",this.state.userName);
                    AsyncStorage.setItem("Email", this.state.userEmail);
                    AsyncStorage.setItem("UserPic", this.state.userPic);

      //  SharedPreferences.setItem("UserName", this.state.userName);
      //       SharedPreferences.setItem("Email", this.state.userEmail);
      //       SharedPreferences.setItem("UserPic", this.state.userPic);
      console.log("Picture"+ this.state.userPic + "Name" + this.state.userName + "Email" + this.state.userEmail);
    }
  }