繁体   English   中英

更改网站域后,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。 错误日志是非常有用的信息来源:)

我做完之后就像魅力一样。 没有更多的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM