2015-09-21 145 views
0

對我的英語不好的藉口...Facebook的api返回錯誤

我下載並安裝了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>'; 

     ?> 

腳本「FB-登錄-callback.php」

<?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'); 

?> 

錯誤返回: 訪問令牌元數據包含意外的應用程序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

它看起來像你的Facebook \ Facebook設置和您的訪問令牌使用不同的應用程序ID。確保您在所有代碼中都使用了正確的應用程序ID。 – daviddoran

+0

數組$ config尚未在您的代碼中定義。 –

回答

1

您嘗試訪問$配置[「APP_ID」]變量,但是你有沒有定義。 試試這個:

$config =[ 
    'app_id' => '88xxxx29', 
    'app_secret' => ‘xxxxxxxxxx', 
    'default_graph_version' => 'v2.2', 
    ]; 
$fb = new Facebook\Facebook ($config);