我的英语不好的借口......
我下载并安装了facebook连接API。我被重定向到Facebook但是一旦在facebook上授权我遇到以下错误:'我的错误'
脚本' index.php'
<?php
require_once __DIR__ . '/facebook_api/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '88xxxx29',
'app_secret' => 'xxxxxxxxxx',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_location', 'user_about_me', 'user_birthday', 'user_actions.books']; // optional
$loginUrl = $helper->getLoginUrl('https://www.mywebsite.fr/fb-login-callback.php', $permissions);
echo '<a href="' . $loginUrl . '">Connexion</a>';
?>
脚本&#39; fb-login-callback.php&#39;
<?php
session_save_path("sessions/");
session_start();
require_once __DIR__ . '/facebook_api/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '88xxxx29',
'app_secret' => ‘xxxxxxxxxx',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
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)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId($config['app_id']);
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://example.com/members.php');
?>
返回错误: Acces令牌元数据包含意外的应用ID。
但是脚本中指定的id是正确的......
Metadata
object(Facebook\Authentication\AccessTokenMetadata)#13 (1) { ["metadata":protected]=> array(7) { ["app_id"]=> string(15) "88xxxx29" ["application"]=> string(13) “mywebsite" ["expires_at"]=> object(DateTime)#17 (3) { ["date"]=> string(26) "2015-11-20 17:55:52.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(12) "Europe/Paris" } ["is_valid"]=> bool(true) ["issued_at"]=> object(DateTime)#18 (3) { ["date"]=> string(26) "2015-09-21 18:55:52.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(12) "Europe/Paris" } ["scopes"]=> array(6) { [0]=> string(13) "user_birthday" [1]=> string(13) "user_location" [2]=> string(13) "user_about_me" [3]=> string(5) "email" [4]=> string(18) "user_actions.books" [5]=> string(14) "public_profile" } ["user_id"]=> string(17) "10207379116973089" } }
**Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookSDKException' with message 'Access token metadata contains unexpected app ID.'** in /home/wbs/www/facebook_api/Authentication/AccessTokenMetadata.php:329 Stack trace: #0 /home/wbs/www/fb-login-callback.php(53): Facebook\Authentication\AccessTokenMetadata->validateAppId(NULL) #1 {main} thrown in /home/wbs/www/facebook_api/Authentication/AccessTokenMetadata.php on line 329
答案 0 :(得分:1)
您尝试访问$ config ['app_id']变量,但您尚未定义。 试试这个:
$config =[
'app_id' => '88xxxx29',
'app_secret' => ‘xxxxxxxxxx',
'default_graph_version' => 'v2.2',
];
$fb = new Facebook\Facebook ($config );