简体   繁体   English

为什么$ helper-> getSessionFromRedirect()总是返回NULL?

[英]Why is $helper->getSessionFromRedirect() always returning NULL?

I had this code and it was working fine for months. 我有此代码,并且可以正常工作数月。 Suddenly, the production environment stopped working, where $helper->getSessionFromRedirect() is now always returning a NULL value. 突然,生产环境停止工作,其中$helper->getSessionFromRedirect()现在始终返回NULL值。

login form page (index.php) 登录表单页面 (index.php)

require 'vendor/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;

FacebookSession::setDefaultApplication(123456, 'a1b2c3d4e5');

$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');
$scope = array('email');
$loginUrl = $helper->getLoginUrl($scope);

echo '<a href="'.$loginUrl.'">Login</a>';

login processing (login.php) 登录处理 (login.php)

require 'vendor/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

FacebookSession::setDefaultApplication(123456,'a1b2c3d4e5');

$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php');

try {
    # success
    $session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
    # when Facebook returns an error
} catch(\Exception $ex) {
    # when validation fails or other local issues
}

if($session) {
    # do something with user data
    $me = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());

    # redirect to user profile page
    header('Location: http://domain.com/profile.php');
    exit();
} else {
    # $session is always NULL with this code :(
}

This is working on my test App, but it's not working with my production App ID. 适用于我的测试应用,但不适用于我的生产应用ID。 The only difference between those are, of course, the App ID and Secret ID values. 它们之间的唯一区别当然是应用程序ID和秘密ID值。 I already made sure I was following the Facebook instructions . 我已经确定要遵守Facebook上的说明 Also, I'm using Facebook PHP SDK 4 's last version. 另外,我正在使用Facebook PHP SDK 4的最新版本。

After a long diagnosis, I found out that there was a discrepancy. 经过长时间的诊断,我发现有差异。 Since I was using Cloudflare's SSL feature, the original $helper = new FacebookRedirectLoginHelper('http://domain.com/login.php'); 由于我使用的是Cloudflare的SSL功能,因此原始的$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php'); request was made with http:80, but the 2nd $helper = new FacebookRedirectLoginHelper('http://domain.com/login.php'); 请求是使用http:80进行的,但是第二个$helper = new FacebookRedirectLoginHelper('http://domain.com/login.php'); was made with https:443 ... this returns a no match kind of signature to Facebook, which causes the NULL value in the $session variable. 是使用https:443制作的...这会向Facebook返回一种不匹配的签名,这将导致$session变量中的值为NULL。

TL;DR do not mix http and https when making requests to fb. TL; DR在向fb发出请求时不要将http和https混合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM