React-native fbsdk获取名称和电子邮件

时间:2018-05-01 21:34:28

标签: facebook facebook-graph-api react-native fbsdk react-native-fbsdk

我正在使用react-native-fbsdk。我想获取用户名和电子邮件,以便在用户注册到应用时向用户显示,以便为应用创建个人资料,但不会显示任何数据。如何获取这些数据以及如何在不同的屏幕中显示它们。

这是我的代码



    if (TextUtils.isEmpty(chickenBlanche)) {
        textView.setVisibility( View.GONE );
    } else {
        textView.setVisibility( View.VISIBLE );
    }


    if (TextUtils.isEmpty(philadelphiaCheese)) {
        textView.setVisibility( View.GONE );
    } else {
        textView.setVisibility( View.VISIBLE );
    }






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


const {
  LoginManager,
} = FBSDK;




谢谢朋友们

2 个答案:

答案 0 :(得分:1)

我在使用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);
      }); 
    

    };

  4. 我正在检索我的facebook登录数据

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

    结果包含用户名,电子邮件,图片和...身份凭证

答案 1 :(得分:0)

如果您不想使用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);
    }
  }