我从未使用过 Facebook SDK 4,我在这里查看了很多答案,但都没有成功。
我正在尝试使用 api 的 facebook 登录部分,但是每次我从 facebook 重新定向回来时,都没有存储会话。这是代码:
<?php
// I am using autoload just to make sure it was including the requested classes, it makes no difference if i include them separately the outcome is the same.
function __autoload($classname) {
$filename = "../Facebook/". end(explode("\\",$classname)) .".php";
include_once($filename);
}
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
// start session
session_start();
// init app with app id and secret -
FacebookSession::setDefaultApplication( '****','****' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://dev.mydomain.co.uk/fb/index.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues
}
if ($session) {
// Logged in
print('loggedin');
} else {
print('no session');
$loginUrl = $helper->getLoginUrl();
?>
<a href="<?=$loginUrl?>">login</a>
<?php
}
我已经替换了应用程序 ID 和密码,并且重定向 uri 是正确的,但我只是在这里替换了域。
它返回到我的登录页面,只是总是打印“无会话”。所以看起来$session = $helper->getSessionFromRedirect();
没有做我需要做的事情。
我从 facebook 开发者网站获得了这段代码。
我尝试过的事情:
- 单独包含类文件而不是自动加载
- 将整个内容移动到一个单独的目录中,而 index.php 文件中没有任何其他内容
- 删除了我的 .htaccess 文件,以确保它不会干扰 GET 请求。
- 我还尝试了一些不同的教程,即http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/完全使用他们的代码(但替换了 app_id、app_sec 和 url 位)——但我仍然没有返回任何会话
我有点茫然,所以如果有人有这方面的经验,那会很有帮助