【问题标题】:Graph returned an error: This authorization code has been used图返回错误:此授权码已被使用
【发布时间】:2023-03-26 10:13:01
【问题描述】:

在尝试使用 Facebook 登录时,我遇到了这个错误:

图表返回错误:此授权码已被使用。

请建议我该怎么做?我已将所有 app_id 和 app_secret 正确地放入代码中。

config.php
if (!session_id()) {
    session_start();
}
require_once('Facebook/autoload.php');
$fb = new Facebook\Facebook([
  'app_id' => 'XXXXXXXX', // Replace {app-id} with your app id
  'app_secret' => 'XXXXXXXXXX',
  'default_graph_version' => 'v3.2',
  ]);
  $helper = $fb->getRedirectLoginHelper();

回调.php

require_once("config.php");
$helper = $fb->getRedirectLoginHelper();
if(isset($_GET['state']))
{
    $helper->getPersistentDataHandler()->set('state', $_GET['state']);
}
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 (!$accessToken) {
    header("Location: ".$siteurl."login.php");
    exit;
  }
$oAuth2Client = $fb->getOAuth2Client();
if (! $accessToken->isLongLived()) {
    $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
    $response = $fb->get("/me?fields=id,email", $accessToken);
    $userData = $response->getGraphNode()->asArray();
    $_SESSION['userData'] = $userData;
    $_SESSION['accessToken'] = (string) $accessToken;
    header("Location: ".$siteurl."profile.php");
    exit;
}

【问题讨论】:

  • 感谢纠正我是新来的。我们会帮我解决我的错误
  • 看起来您调用了两次getRedirectLoginHelper 方法。而且您对 state 参数的处理似乎完全错误 - 当您从 Facebook 重定向回您的应用程序时,应该 in 已经在您的会话中,您似乎正在将它 写入那个时候的会话,这完全违背了它的目的。
  • 我从 callback.php 文件中删除了 getRedirectLoginHelper。仍然显示相同的错误。如果我没有处理该状态条件,它会给我 Facebook SDK 返回错误:跨站点请求伪造验证失败。持久数据中缺少必需的参数“状态”。错误
  • 看起来您一开始就没有正确处理会话。将 session_start 包装到 if (!session_id()) 没有什么意义 - 如果删除它,然后开始会话。
  • 如果条件仍然显示相同的错误,我删除了会话。

标签: php facebook-graph-api facebook-php-sdk


【解决方案1】:

现在我在 Facebook 开发者网站上使用了这个。

<!DOCTYPE html>
<html>
<head>
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
</head>
<body>
<script>
  function statusChangeCallback(response) {  // Called with the results from FB.getLoginStatus().
    console.log('statusChangeCallback');
    console.log(response);                   // The current login status of the person.
    if (response.status === 'connected') {   // Logged into your webpage and Facebook.
      testAPI();  
    } else {                                 // Not logged into your webpage or we are unable to tell.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this webpage.';
    }
  }
  function checkLoginState() {               // Called when a person is finished with the Login Button.
    FB.getLoginStatus(function(response) {   // See the onlogin handler
      statusChangeCallback(response);
    });
  }
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '{app-id}',
      cookie     : true,                     // Enable cookies to allow the server to access the session.
      xfbml      : true,                     // Parse social plugins on this webpage.
      version    : '{api-version}'           // Use this Graph API version for this call.
    });
    FB.getLoginStatus(function(response) {   // Called after the JS SDK has been initialized.
      statusChangeCallback(response);        // Returns the login status.
    });
  };
  (function(d, s, id) {                      // Load the SDK asynchronously
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "https://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
  function testAPI() {                      // Testing Graph API after login.  See statusChangeCallback() for when this call is made.
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
    });
  }
</script>
//  The JS SDK Login Button 
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
<div id="status">
</div>
</body>
</html>

现在我想重定向我的页面。登录后如何重定向页面?

【讨论】:

    猜你喜欢
    • 2013-02-20
    • 2014-01-06
    • 2016-06-21
    • 2016-06-22
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    相关资源
    最近更新 更多