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

帅比萌擦擦* 提交于 2019-12-11 12:21:34

问题


Note: Before I start, I checked all the other questions on this site which pertain to the same exception and none of the answers worked for me.

I've been having trouble with the facebook sdk and facebook login for a few days now. My website had to change domain (from benotes.com to notepit.com). Since I've changed this, I can no longer use facebook login and get the error "This authorization code has been used".

I changed everything in my facebook app to the new domain, and even tried creating a new facebook app from scratch for the new domain. However, I am still getting the error. Here is my code:

/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);

?>

This gives me a URL ($fbLink) which I put in an a's href. When the user clicks the link, he/she is redirected to facebook login and then redirected to this page:

/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'];

?>

All that this page returns is "Graph returned an error: This authorization code has been used.", meaning it's from the very first "try" if that helps.

I would also post screenshots of the facebook app but as I said it was working before the domain change and now it's not anymore and I have a new facebook app.

Whoever finds me an answer I will respect forever.


回答1:


I just resolved the same issue. Turned out I had to match the timezone in php.ini (date.timezone) and restart httpd. The error logs is a very helpful source of information :)

Worked like a charm after I did that. No more errors.



来源:https://stackoverflow.com/questions/35812190/after-changing-website-domain-facebook-login-returns-error-this-authorization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!