0

i am having this trouble with getting my login with Facebook feature to work. I have a button in my login page that links to the following .php file. If the login is successful i send it to a welcome.php just with a sentence for testing. In my facebook developer settings i have put under domain localhost, same for website URL since i am working in localhost.

Problem? when i click it i get an error saying my session is not initiated. I don't know what to do. Error when clicking login with facebook button

    <?php  

    session_start();
    error_reporting(E_ALL);
    ini_set('display_errors', true);
    ini_set('display_startup_errors', true);
    ini_set('memory_limit', '-1');

    //include o ficheiro do autoloader do SDK
    require_once "LoginFB/lib/Facebook/autoload.php";

    //include required libraries
    use Facebook\Facebook;
    use Facebook\Exceptions\FacebookResponseException;
    use Facebook\Exceptions\FacebookSDKException;


    $appId = 'myID'; //Facebook App ID
    $appSecret = 'mySecret'; //Facebook App Secret
    $redirectURL = 'http://localhost/Driveaway/PedroQuinta/welcome.php'; //Callback URL
            $fbPermissions = array('email');  //Optional permissions

    $fb = new Facebook(array(
        'app_id'=>$appId,
        'app_secret'=>$appSecret,
        'default_graph_version' => 'v2.9',

    ));

    //get redirect login helper
    $helper = $fb->getRedirectLoginHelper();

    //Try to get access token
    try {
        //already login
        if(isset($_SESSION['face_access_token'])){
            $accessToken = $_SESSION['face_access_token'];
        }else{
            $accessToken = $helper->getAccessToken();
        }

        if(isset($accessToken)){
            if(isset($_SESSION['face_access_token'])){
                $fb->setDefaultAccessToken($_SESSION['face_access_token']);
            }else{
                //put short-lived access token in session
                $_SESSION['face_access_token'] =(string)$accessToken;

                // OAuth 2.0 client handler helps manage access tokens
                $oAuth2Client = $fb->getOAuth2Client();

                //Exchanges a short-lived access token for a long-lived one
                $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['face_access_token']);
                $_SESSION['face_access_token']=(string)$longLivedAccessToken;

                //Set default access token to be used in script
                $fb->setDefaultAccessToken($_SESSION['face_access_token']);

            }

            //redirects the user back to the same page if url has "code" parameter in query string

            if(isset($_GET['code'])){
                //getting user facebook profile info
                try {
                    $profileRequest = $fb->get('/me?fields=name, email');
                    $fbUserProfile = $profileRequest->getGraphNode()->asArray();
                    //here you can redirect to your homepage.
                    echo "<pre/>";
                    print_r($fbUserProfile);
                } catch (FacebookResponseException $e) {
                    echo 'Graph returned an error: ' . $e->getMessage();
                    session_destroy();
                    // Redirect user back to app login page
                    header("Location: ./");
                    exit;
                } catch (FacebookSDKException $e) {
                    echo 'Facebook SDK returned an error: ' . $e->getMessage();
                    exit;
                }
            }
        }else{
            //get login url

            $loginUrl = $helper->getLoginUrl($redirectURL, $fbPermissions);
            header("Location: ".$loginUrl);
        }
    } catch (FacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch (FacebookSDKException $e) {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }

   ?>
PedroQ
  • 15
  • 5
  • You can't use `localhost` as your server, because every single server is `localhost`. You will have to use your actual outward facing IP address if you want to test it locally. – mopsyd Dec 16 '17 at 01:47
  • @mopsyd that's not true, there is no problem testing Facebook login on only locally available domains, because all the relevant stuff happens inside the user's browser. (Testing Open Graph stuff or web hooks, where Facebook actually needs to make a request to your server, is a different thing.) – CBroe Dec 16 '17 at 09:53
  • That is true if you are doing frontend Oauth. OP is not doing frontend though. – mopsyd Dec 16 '17 at 12:08
  • See [this facebook example](https://developers.facebook.com/docs/php/howto/example_facebook_login), where `https://example.com/fb-callback.php` requires you to provide your replacement domain for `example.com`. This is just an SSO handshake, not anything special, but since it's backend driven, it does require your domain and/or IP. – mopsyd Dec 16 '17 at 12:13

0 Answers0