gpt4 book ai didi

php - Facebook PHP SDK v4.0.0 : $session is not set even after authorizing facebook from the login URL

转载 作者:可可西里 更新时间:2023-11-01 00:44:33 35 4
gpt4 key购买 nike

更新:问题现已解决。我所做的就是将这段代码放在一个不同的 php 文件中:

<?php
Function __autoload($classname) {
$filename = "./".$classname .".php";
require_once($filename);
}

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

session_start();

FacebookSession::setDefaultApplication('257772307761170','***');

$helper = new FacebookRedirectLoginHelper('http://localhost/parqueeste/login.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) {
echo 'Yeah!!';
}
?>

原始消息:我正在使用适用于 PHP 的 Facebook SDK v4.0.0 创建“通过 Facebook 登录”。但是即使在用户点击登录 URL 之后,$session 变量也没有定义,所以我无法访问用户的数据。这是我的代码:

<?php
session_start();
function __autoload($classname) {
$filename = "./".$classname .".php";
require_once($filename);
}

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

FacebookSession::setDefaultApplication('257772307761170','****'); //I have the right
app secret on my code, of course.

$helper = new FacebookRedirectLoginHelper('http://localhost/parqueeste/');
$loginUrl = $helper->getLoginUrl();


if(isset($session) AND $session) { // <--- I use isset() so I don't
get a warning everytime I load my site

try {

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

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

} catch(FacebookRequestException $e) {

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

}

}
?>

提前致谢!

最佳答案

这里的实际问题是,当您调用 getLoginUrl() 方法时,例如

 $helper = new FacebookRedirectLoginHelper('http://localhost/parqueeste/');
$loginUrl = $helper->getLoginUrl();

它改变了状态变量。在您的原始问题中(在您对其进行编辑以包含您的修复程序之前),您在验证原始代码中的响应之前调用了此方法,然后它使 CSRF 检查无效,简而言之,用户没有已登录。我遇到了完全相同的问题,最好的办法是像您在此处所做的那样拥有一个单独的登录页面。或者在完成所有有效 session /图形对象响应的检查后生成登录 url。

希望这对遇到同样问题的其他人有所帮助,因为它让我烦恼了好几天而且没有真正记录下来,并且与以前的 api 相比是一个很大的变化。您需要查看 SDK 以查看发生了什么

关于php - Facebook PHP SDK v4.0.0 : $session is not set even after authorizing facebook from the login URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23409869/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com