0

I am developing a plugin where I am trying to add facebook login. I have added both of my login button code as well as response code ( getting logged in user info after redirect ) in the same page and in the same function.

This is my login button code:

        if (!session_id()) {
            session_start();
        }
        $fb = new Facebook\Facebook([
          'app_id' => 'xxxxxxxx', // Replace {app-id} with your app id
          'app_secret' => 'xxxxxxxxx',
          'default_graph_version' => 'v2.5',
          ]);

        $helper = $fb->getRedirectLoginHelper();

        $permissions = ['email']; // Optional permissions
        $loginUrl = $helper->getLoginUrl('example.com/test', $permissions);

        echo '<div class="social-login-button"><div class="fb-login">';
        ?>
        <script type="text/javascript">
        function MM_openBrWindow(theURL,winName,features) {
        window.open(theURL,winName,features);
        }
        </script>
        <a onclick="MM_openBrWindow('<?php echo $loginUrl; ?>','google','scrollbars=yes,width=650,height=500')">Log in with Facebook!</a></div></div>

And the below code is for the response after user is logged in:

        require 'vendor/autoload.php';
        if (!session_id()) {
            session_start();
        }
        //print_r(session_id());
        if($_GET['code'])
        {
        $fb = new Facebook\Facebook([
         'app_id' => 'xxxxxxxxxx',
         'app_secret' => 'xxxxxxxxxx',
         'default_graph_version' => 'v2.5',
        ]);
        $helper = $fb->getRedirectLoginHelper();
        $permissions = ['email']; // optional
        try {

            //$permissions = ['email']; // Optional permissions
            $loginUrl = $helper->getLoginUrl('https://demo.surplusthemes.com/response/', $permissions);
            $accessToken = $helper->getAccessToken($loginUrl);

        } catch(Facebook\Exceptions\facebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
          exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
          exit;
        }

        if (isset($accessToken)) {  echo 'test'.$accessToken;
        if (isset($_SESSION['facebook_access_token'])) {
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
        } else {
        // getting short-lived access token
        $_SESSION['facebook_access_token'] = (string) $accessToken;
          // OAuth 2.0 client handler
        $oAuth2Client = $fb->getOAuth2Client();
        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
        // setting default access token to be used in script
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
        }
        // redirect the user to the profile page if it has "code" GET variable
        if (isset($_GET['code'])) {
        }
        // getting basic info about user
        try {
        $profile_request = $fb->get('/me?fields=name,first_name,last_name,email');
        $requestPicture = $fb->get('/me/picture?redirect=false&height=200'); //getting user picture
        $picture = $requestPicture->getGraphUser();
        $profile = $profile_request->getGraphUser();
        $fbid = $profile->getProperty('id');           // To Get Facebook ID
        $fbfullname = $profile->getProperty('name');   // To Get Facebook full name
        $fbemail = $profile->getProperty('email');    //  To Get Facebook email
        $fbpic = "<img src='".$picture['url']."' class='img-rounded'/>";
        # save the user nformation in session variable
        $_SESSION['fb_id'] = $fbid.'</br>';
        $_SESSION['fb_name'] = $fbfullname.'</br>';
        $_SESSION['fb_email'] = $fbemail.'</br>';
        $_SESSION['fb_pic'] = $fbpic.'</br>';
        } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        //session_destroy();
        // redirecting user back to app login page
        //header("Location: ./");
        exit;
        } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
        }
        } else {
        // replace your website URL same as added in the developers.Facebook.com/apps e.g. if you used http instead of https and you used            
        $loginUrl = $helper->getLoginUrl('example.com/test', $permissions);
        echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
        }
        print_r($_SESSION);
        }

The issue is I am unable to get the logged in user info such as name, photo etc.

There must be something I am missing :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.