Search code examples
react-nativereact-native-fbsdk

react-native fbsdk is returning email undefined despite of all the permissions granted


I m trying to get information through the facebook sdk but so far I'm getting only the id and the name of the user. I do have granted the premission but still the email is popping out like name and id. I have almost tried everything.

Any help would be appreciated.

Thanks in advance

      import React, { Component } from 'react'
       import { View, TouchableOpacity, Text } from 'react-native';
         import { LoginButton, AccessToken, LoginManager, GraphRequest, 
           GraphRequestManager } from 'react-native-fbsdk';

              export default class App extends Component {
               constructor() {
              super();
                this._fbAuth = this._fbAuth.bind(this);
                          }
                      _fbAuth = () => {
               var that = this;
             LoginManager.logInWithReadPermissions(['public_profile', 
             'email']).then(function (result) {
                if (result.isCancelled) {
           console.log("Login Cancel");
           alert("Login Cancel")
              //debugger
            }
             else {
               console.log("Login AccessToken");
               alert("Login AccessToken");
              AccessToken.getCurrentAccessToken().then(
                       (data) => {
        //      alert("Get AccessToken");
        //debugger
              let accessToken = data.accessToken;
             alert(accessToken.toString());
               const responseInfoCallback = (error, result) => {
            setTimeout(() => {
            if (error) {
              alert('Errorrrrrrrrr');

            }
            else {
              if (result.email == undefined) {
                alert("Error ,Email address is required");
                // stuck at email. result.name and result.id gives correct results

              }
              else {
                alert(result.email);
              }
            }
          }, 200
          );
        }
        const infoRequest = new GraphRequest(
          '/me',
          {
            accessToken: accessToken,
            prameters: {
              fields: {
                string: 'email , name,first_name, middle_name, last_name'
              }
            }
          },
          responseInfoCallback
        );
        new GraphRequestManager().addRequest(infoRequest).start();

               })
                  }
                  },
           function (error) {
            console.log("some error occured!!", error);
          })

            }

           render() {
         return (
             <View>
             <TouchableOpacity onPress={() => this._fbAuth()}>
               <Text>  Press me</Text>
           </TouchableOpacity>
            </View>
        );
             }

                   }

result.email should not be undefined.


Solution

  • You can just use the accessToken provided by fbsdk. Just get the accessToken, once you receive the accessToken, You can make request to facebook graph api's to receive data related to that user using the accessToken.

    https://developers.facebook.com/docs/graph-api/using-graph-api

    Here is the full documentation and It also includes the accessToken. It would help you. I also do the same.