[英]Can't get the access token in facebook sdk 5 login using php?
I hosted my files in heroku hosting. 我将文件托管在heroku托管中。 Facebook login callback file throws both the exceptions. Facebook登录回调文件将同时引发这两个异常。
First exception 第一个例外
Response Exception: Can't Load URL: The domain of this URL isn't included in the app's domains. 响应异常:无法加载URL:此URL的域未包含在应用程序的域中。 To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings. 为了能够加载此URL,请将应用程序的所有域和子域添加到应用程序设置的“应用程序域”字段中。
Second Exception after reloading 重新加载后的第二个异常
SDK Exception: Cross-site request forgery validation failed. SDK异常:跨站点请求伪造验证失败。 Required param "state" missing from persistent data. 永久数据中缺少必需的参数“状态”。
fb-callback.php FB-callback.php
<?php
require_once "config.php";
try {
$accessToken = $helper->getAccessToken();
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
echo "Response Exception: " . $e->getMessage();
exit();
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
echo "SDK Exception: " . $e->getMessage();
exit();
}
if (!$accessToken) {
header('Location: login.php');
exit();
}
$oAuth2Client = $FB->getOAuth2Client();
if (!$accessToken->isLongLived())
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
$response = $FB->get("/me?fields=id,name,email", $accessToken);
$userData = $response->getGraphNode()->asArray();
$_SESSION['userData'] = $userData;
$_SESSION['access_token'] = (string) $accessToken;
header('Location: index.php');
exit();
?>
I tried removing the try catch block and just used the statement 我尝试删除try catch块,只是使用了以下语句
$accessToken = $helper->getAccessToken();
This caused error 500 这导致错误500
This page isn't working 此页面无效
fanism.herokuapp.com is currently unable to handle this request. fanism.herokuapp.com当前无法处理此请求。 HTTP ERROR 500 HTTP错误500
config.php (used in fb-callback.php) config.php (用于fb-callback.php)
<?php
session_start();
require_once "Facebook/autoload.php";
$FB = new \Facebook\Facebook([
'app_id' => '952208124958752',
'app_secret' => '9088c861803fda4dadbe950915ae14f2',
'default_graph_version' => 'v2.10'
]);
$helper = $FB->getRedirectLoginHelper();
?>
created composer.json file in the project folder with the following code 使用以下代码在项目文件夹中创建了composer.json文件
{
"require" : {
"facebook/graph-sdk" : "~5.0"
}
}
run command in project folder(command prompt) 在项目文件夹中运行命令(命令提示符)
composer install
facebook sdk folder( vendor ) download and from that vendor folder, take out the Facebook folder and paste it in project.(deleted the previous manually downloaded Facebook folder) facebook sdk文件夹( 供应商 )下载,然后从该供应商文件夹中取出Facebook文件夹并将其粘贴到项目中。(删除了以前手动下载的Facebook文件夹)
then I uploaded that files normally to Heroku. 然后我通常将该文件上传到Heroku。 Finally solved. 终于解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.