Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not getting Email id from fbsdk in react native

I am getting login success and accessToken . I made

I tried

LoginManager.logInWithReadPermissions([
      'email',
      'public_profile',
      'user_likes',
    ])

then this

LoginManager.logInWithPublishPermissions([
  'publish_actions',
]).then((result) => {
  if (result.isCancelled) {
    console.log('Login cancelled');
  } else {
    AccessToken.getCurrentAccessToken().then((data) => {
      const accessToken = data.accessToken;
      const responseInfoCallback = (error, result) => {
        if (error) {
          console.log(error);
          console.log('Error fetching data=', error.toString());
        } else {
          console.log('Success fetching data=', result.toString());
        }
      };
      const infoRequest = new GraphRequest(
        '/me',
        {
          accessToken,
          parameters: {
            fields: {
              string: 'email,name,first_name,middle_name,last_name',
            },
          },
        },
        responseInfoCallback,
      );
      new GraphRequestManager().addRequest(infoRequest).start();
    });
  }
});

'Login success with permissions', 'email,public_profile' and user profile data is

{ first_name: 'XXXXXX',
  name: 'XXXXXX YYYYY',
  last_name: 'YYYYYY',
  id: '1319989423636921696328130092' }

But not getting user email id , what is wrong here. how to get email id after login in Facebook in app.

like image 939
Sport Avatar asked Sep 12 '25 02:09

Sport


1 Answers

This is how I am doing it in my project and successfully retrieving email

LoginManager.logInWithReadPermissions(['public_profile', 'email', 'user_friends']).then(
            (result) => {
                if (result.isCancelled) {
                    console.log('Login cancelled')
                } else {
                    AccessToken.getCurrentAccessToken().then(
                        (data) => {
                            this._fbHome();// Navigatin to next screen
                        }
                    )
                }
            },
            (error) => {
                console.log('Login fail with error: ' + error)
            }
        )

Then after navigating to next screen I am extracting user data

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);
    }
  }
like image 193
Paras Watts Avatar answered Sep 14 '25 18:09

Paras Watts