dtye7921 2016-09-22 10:12
浏览 87
已采纳

移动浏览器中的Facebook登录错误

I'm using API Facebook to login on my website but.. login from desktop browser works fine, login from mobile browser return an error.

The error message from mobile browser is: Facebook SDK returned an error: Cross-site request forgery validation failed. Required param "state" missing from persistent data.

In login page I used this code to generate the link to facebook (I'm using the Facebook PHP SDK)

login

    if(!session_id()) {
        session_start();
    }
    $fb = new Facebook\Facebook([
      'app_id' => APP_ID,
      'app_secret' => APP_SECRET,
      'default_graph_version' => 'v2.2'
    ]);

    $helper = $fb->getRedirectLoginHelper();

    $permissions = ['email']; // Optional permissions
    $loginUrl = $helper->getLoginUrl(SERVER_CALLBACK, $permissions);

    $facebook = htmlspecialchars($loginUrl);

This is the callback

callback

    if(!session_id()) {
        session_start();
    }
    $fb = new Facebook\Facebook([
        'app_id' => APP_ID,
        'app_secret' => APP_SECRET,
        'default_graph_version' => 'v2.2',
        'persistent_data_handler'=>'session'
    ]);

    $helper = $fb->getRedirectLoginHelper();

    try {
        $accessToken = $helper->getAccessToken();
    } 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)) {
        if ($helper->getError()) {
            header('HTTP/1.0 401 Unauthorized');
            echo "Error: " . $helper->getError() . "
";
            echo "Error Code: " . $helper->getErrorCode() . "
";
            echo "Error Reason: " . $helper->getErrorReason() . "
";
            echo "Error Description: " . $helper->getErrorDescription() . "
";
        } else {
            header('HTTP/1.0 400 Bad Request');
            echo 'Bad request';
        }
        exit;
    }

    // Logged in

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

    // Get the access token metadata from /debug_token
    $tokenMetadata = $oAuth2Client->debugToken($accessToken);

    // Validation (these will throw FacebookSDKException's when they fail)
    $tokenMetadata->validateAppId(APP_ID);
    // If you know the user ID this access token belongs to, you can validate it here
    $tokenMetadata->validateExpiration();

    if (! $accessToken->isLongLived()) {
        // Exchanges a short-lived access token for a long-lived one
        try {
            $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
        } catch (Facebook\Exceptions\FacebookSDKException $e) {
            echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>

";
            exit;
        }
    }

I'm wonder because everything is working in desktop navigation, but using a mobile browser something is going wrong.

  • 写回答

1条回答 默认 最新

  • douzhouhan4618 2016-09-24 11:47
    关注

    The problem was during the generation of session.

    Here the wrong code:

    if (ini_set('session.use_only_cookies', 1) === FALSE) exit();
    // Forces sessions to only use cookies.
    ini_set( 'session.cookie_httponly', 1 );
    // This stops JavaScript being able to access the session id.
    // Activate if you use a HTTPS connection
    ini_set( 'session.cookie_secure', 0 );
    // Sets the session name to the one set above.
    session_name(NAME_SESSION);
    session_set_cookie_params(3600 * 12);
    session_start();
    session_regenerate_id(TRUE); // <- This is the error
    

    Because the function session_regenerate_id does not handle unstable network well. e.g. Mobile and WiFi network. Therefore, you may experience lost session by calling session_regenerate_id. Here the solution

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog