我刚从facebook复制了以下脚本 https://developers.facebook.com/docs/php/gettingstarted
我使用composer
下载了主代码 <?php
session_start();
require_once '../../Facebook/autoload.php';
print_r($_SESSION);
//if(!isset($_SESSION['user']) || ($_SESSION['user'] < 1) || ($_SESSION['user'] == '')){
$fb = new Facebook\Facebook([
'app_id' => '1444966872674589',
'app_secret' => '54362fa0c423jdui348758ea172537fb',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getCanvasHelper();
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)) {
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
} 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;
}
echo 'Logged in as ' . $userNode->getName();
print_r($userNode->getName());
}
//}
但是上面的代码会抛出错误
Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookSDKException' with message 'Signed request has an invalid signature.'
我已阅读此链接
Integrating facebook in php giving invalid signed request with oAuth data
和
Integrating facebook php sdk in Facebook Canvas App giving Blank page or oauthData error
但它并没有解决我的问题
请帮助我解决问题
答案 0 :(得分:1)
如果您从网站上调用脚本,可以试试这个:
替换:
$helper = $fb->getCanvasHelper();
使用:
$helper = $fb->getRedirectLoginHelper();
也取代:
if (isset($accessToken)) {
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
} 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;
}
echo 'Logged in as ' . $userNode->getName();
print_r($userNode->getName());
}
使用:
$permissions = 'list of facebook permissions';///optional
if (isset($accessToken)) {
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
} 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;
}
echo 'Logged in as ' . $userNode->getName();
print_r($userNode->getName());
}else{
$loginUrl = $helper->getLoginUrl('url-of-calling-script.php', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
}