Facebook app fails to get Session

浪尽此生 提交于 2019-12-12 18:07:24

问题


I'm developing a very basic PHP app on Heroku for Facebook which shows very basic user info like Name, Profile Picture, but the app halts in getToken method !
I've tried the app after logging in my profile still the same message :

Here are the codes

composer.json

{}

index.php

    session_start();
    require_once( 'Facebook/FacebookSession.php' );
    require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
    require_once( 'Facebook/FacebookRequest.php' );
    require_once( 'Facebook/FacebookResponse.php' );
    require_once( 'Facebook/FacebookSDKException.php' );
    require_once( 'Facebook/FacebookRequestException.php' );
    require_once( 'Facebook/FacebookAuthorizationException.php' );
    require_once( 'Facebook/GraphObject.php' );
    require_once( 'Facebook/GraphUser.php' );
    require_once( 'Facebook/GraphSessionInfo.php' );

    use Facebook\FacebookSession;
    use Facebook\FacebookRedirectLoginHelper;
    use Facebook\FacebookRequest;
    use Facebook\FacebookResponse;
    use Facebook\FacebookSDKException;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookAuthorizationException;
    use Facebook\GraphObject;
    use Facebook\GraphUser;
    use Facebook\GraphSessionInfo;
FacebookSession::setDefaultApplication($id, $secret);
$helper = new FacebookRedirectLoginHelper('http://facebook.com');

try{
    $session = $helper->getSessionFromRedirect();
} catch(Exception $e){  echo 'Error in Session > '.$e;
}

if(isset($_SESSION['token'])){
    $session = new FacebookSession($_SESSION['token']);
    try{
        $session->Validate($id, $secret);
    }catch(FacebookAuthorizationException $e){
    echo 'Error in Token >'.$e;     $session = '';
    }
}

if(isset($session)){
    $_SESSION['token'] = $session->getToken();
    echo "Login Successful<br>";
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request->execute();
    $graph = $response->getGraphObject(GraphUser::className());
    echo "Hi " . $graph->getName();
}
else{
    echo "<a href = " . $helper->getLoginUrl() . ">Login With Facebook</a>";
}

The app always goes to the Login With Facebook ,the last else part !
Where the problem lies ?
I'm completely new to this Heroku and Facebook SDK v4 and after searching for hours did not found any sample app on Facebook for Heroku ~


回答1:


Try changing your code to this and see if it helps:

if ( isset( $_SESSION['token'] ) ) {
    $session = new FacebookSession($_SESSION['token']);
    try{
        $session->Validate($id, $secret);
    }catch(FacebookAuthorizationException $e){
    echo 'Error in Token >'.$e;     $session = '';
        $session = null;
    }
} else {
    $session = null;
}

// if session is null, try getting it from redirect
if ( $session === null ) {
    try{
        $session = $helper->getSessionFromRedirect();
    } catch(Exception $e){  echo 'Error in Session > '.$e;
    }
}

if ( isset( $session ) && $session != null ) {
    $_SESSION['token'] = $session->getToken();
    echo "Login Successful<br>";
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request->execute();
    $graph = $response->getGraphObject(GraphUser::className());
    echo "Hi " . $graph->getName();
}
else{
    echo "<a href = " . $helper->getLoginUrl() . ">Login With Facebook</a>";
}

I've uses a similar method to the code shown here.



来源:https://stackoverflow.com/questions/24477042/facebook-app-fails-to-get-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!