0

This is my code

<?php
    session_start();
    require_once("facebook-sdk/autoload.php");
    require_once("facebook-sdk/src/Facebook/FacebookSession.php");
    require_once("facebook-sdk/src/Facebook/FacebookRequest.php");
    require_once("facebook-sdk/src/Facebook/FacebookResponse.php");
    require_once("facebook-sdk/src/Facebook/FacebookSDKException.php");
    require_once("facebook-sdk/src/Facebook/FacebookRequestException.php");
    require_once("facebook-sdk/src/Facebook/FacebookRedirectLoginHelper.php");
    require_once("facebook-sdk/src/Facebook/FacebookAuthorizationException.php");
    require_once("facebook-sdk/src/Facebook/GraphObject.php");
    require_once("facebook-sdk/src/Facebook/GraphSessionInfo.php");
    require_once("facebook-sdk/src/Facebook/GraphUser.php");
    require_once("facebook-sdk/src/Facebook/Entities/AccessToken.php");
    require_once("facebook-sdk/src/Facebook/HttpClients/FacebookCurl.php");
    require_once("facebook-sdk/src/Facebook/HttpClients/FacebookHttpable.php");
    require_once("facebook-sdk/src/Facebook/HttpClients/FacebookCurlHttpClient.php");

    use Facebook\FacebookSession;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\GraphSessionInfo;
    use Facebook\GraphUser;
    use Facebook\HttpClients\FacebookCurl;
    use Facebook\Entities\AccessToken;
    use Facebook\HttpClients\FacebookHttpable;
    use Facebook\HttpClients\FacebookCurlHttpClient;

    $api_id="452545828240757";
    $api_secret="30be684049..............";   

    FacebookSession::setDefaultApplication($api_id,$api_secret);        
    $session = new FacebookSession('452545828240757|n-4BLMHDlsgsXSvqr-YlUgXGk_8');

    try {
      $response = (new FacebookRequest($session, 'GET', '/me'))->execute();
      $object = $response->getGraphObject();
      echo $object->getProperty('name');
    } 
    catch (FacebookRequestException $ex) {
      echo $ex->getMessage();
    } 
    catch (\Exception $ex) {
      echo $ex->getMessage();
    }
    ?>

After executing the code, I got the following message

"An active access token must be used to query information about the current user."

Please help me guys, I don't what is the problem and I want to get user information

2
Contributors
4
Replies
25
Views
2 Years
Discussion Span
Last Post by Hilal2009
0

By using $session->getToken() you get the access token. For example when you perform the login request you use a script like this:

<?php

session_start();

require_once "credentials.php";
require_once "autoload.php";
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

$redirect_url = "http://localhost:8005/logged.php";

$helper = new FacebookRedirectLoginHelper($redirect_url, $appId, $appSecret);
echo '<a href="' . $helper->getLoginUrl() . '">Login with Facebook</a>';

The logged.php file in the redirect url is this:

<?php

error_reporting(-1);
session_start();

require_once "credentials.php";
require_once "autoload.php";
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

$redirect_url = "http://localhost:8005/logged.php";

FacebookSession::setDefaultApplication($appId, $appSecret);
$helper       = new FacebookRedirectLoginHelper($redirect_url);

try {

    $session = $helper->getSessionFromRedirect();

    if(isset($session))
    {
        # save token to PHP session
        $_SESSION['access_token'] = $session->getToken();

        # show some text
        echo "<p><a href=\"get.php\">Perform a GET request</a></p>";
    }
}

catch(FacebookRequestException $e) {
    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();
}

catch(\Exception $e) {
    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();
}

Which is where you are redirected by Facebook. Then you browse to another page (in the example this is get.php) where you read the token from the PHP session and perform a request against Facebook:

<?php

error_reporting(-1);
session_start();

require_once "credentials.php";
require_once "autoload.php";
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
use Facebook\FacebookRequestException;

FacebookSession::setDefaultApplication($appId, $appSecret);

# Get the token from PHP session:
$session = new FacebookSession($_SESSION['access_token']);

$user_profile = (new FacebookRequest($session, 'GET', '/me'))
                ->execute()
                ->getGraphObject(GraphUser::className());

echo "Name: " . $user_profile->getName();

Note: by using the autoload.php file released in the SDK you don't need to require all class files manually.

Edited by cereal

0

Thank you so much for your help cereal, atleast rightnow I can see some light. But according to what you have said, it seems there is no session genarated and that's why I get a blank page. Please I need your help again
guys. Below is my codes

<?php
    session_start();
    require_once("facebook-sdk/autoload.php");        
    use Facebook\FacebookSession;
    use Facebook\FacebookRequest;
    use Facebook\GraphUser;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookRedirectLoginHelper;    
    use Facebook\GraphObject;    

    $api_id="452545828240757";
    $api_secret="30be684049158093c0de24025a4fec69";
    $url="http://localhost:8000/feedreader/fbindex.php";
    FacebookSession::setDefaultApplication($api_id,$api_secret); 

    $helper=new FacebookRedirectLoginHelper($url); 
    try{
        $session=$helper->getSessionFromRedirect();
        if(isset($session))
        {
            $_SESSION["access_token"]=$session->getToken();
            echo "<a href='".$helper->getLoginUrl()."'>Login with Facebook</a>";
        }
    }
    catch(FacebookRequestException $e)
    {
        echo "Exception occured, code: " . $e->getCode();
        echo " with message: " . $e->getMessage();
    }
    catch (\Exception $e) 
    {
        echo "Exception occured, code: " . $e->getCode();
        echo " with message: " . $e->getMessage();
    }    
?>

Here again is my App

Edited by Hilal2009: forgoten some information

Attachments fbsetup.JPG 36.4 KB
0

Is this the code of the http://localhost:8000/feedreader/fbindex.php script?

The redirect url MUST be the script that will receive the authorization code appended to the link, for example:

http://localhost:8000/feedreader/fbindex.php?code=AUTHORIZATION_CODE 

The value of $_GET['code'] is read by:

$session = $helper->getSessionFromRedirect();

Which enables the Facebook Session by realesing an access token that you must save in some way, for example into a PHP session, otherwise, and this is important, when you browse to another page you will loose it.

In your current code it seems you are trying to save the access token before clicking the access link. Which is hidden because there is no Facebook session at the moment. So try:

if(isset($session))
{
    $_SESSION["access_token"]=$session->getToken();
}

else
{
    echo "<a href='".$helper->getLoginUrl()."'>Login with Facebook</a>";
}

And it should work.

0

Greate thanks to you cereal, I appreciate you so much my friend. Each and everything now goes right......

Again, thank you so much for your help........

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.