更改网站域后,Facebook登录返回错误“此授权代码已被使用”


After changing website domain, facebook login returns error "This authorization code has been used"

注意:在我开始之前,我检查了这个网站上与同一例外相关的所有其他问题,但没有一个答案对我有用。

几天来,我一直在使用Facebook SDK和Facebook登录时遇到问题。我的网站不得不更改域(从 benotes.com 更改为 notepit.com)。由于我已更改此设置,因此我无法再使用Facebook登录并收到错误"此授权代码已被使用"。

我将Facebook应用程序中的所有内容都更改为新域,甚至尝试从头开始为新域创建新的Facebook应用程序。但是,我仍然收到错误。这是我的代码:

/facebook-login.php

<?php
$fb = new Facebook'Facebook([
  'app_id' => '{APP ID}',
  'app_secret' => '{APP SECRET}',
  'default_graph_version' => 'v2.1',
  ]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$fbLink = $helper->getLoginUrl('http://{domain}/facebook-callback.php', $permissions);
?>

这给了我一个URL($fbLink),我把它放在ahref中。当用户单击链接时,他/她将被重定向到Facebook登录,然后重定向到此页面:

/facebook-callback.php

<?php
session_start();
require_once __DIR__.'/vendor/autoload.php';
$fb = new Facebook'Facebook([
  'app_id' => '{APP ID}',
  'app_secret' => '{APP SECRET}',
  'default_graph_version' => 'v2.1',
  ]);
$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({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;
try {
  // Returns a `Facebook'FacebookResponse` object
  $response = $fb->get('/me?fields=id,name', $_SESSION['fb_access_token']);
} catch(Facebook'Exceptions'FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook'Exceptions'FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
?>

此页面返回的所有内容都是"Graph 返回了一个错误:此授权代码已被使用",这意味着它来自第一个"try",如果有帮助的话。

也会发布Facebook应用程序的屏幕截图,但正如我所说,它在域更改之前就可以工作了,现在它不再了,我有一个新的Facebook应用程序。

无论谁找到我的答案,我都会永远尊重。

我刚刚解决了同样的问题。事实证明,我必须匹配php.ini(date.timezone)中的时区并重新启动httpd。错误日志是一个非常有用的信息来源:)

在我这样做之后,就像一个魅力。没有更多的错误。