【问题标题】:Codeigniter, facebook SDK 5, not workingCodeigniter,Facebook SDK 5,不工作
【发布时间】:2015-12-16 00:48:07
【问题描述】:

我已按照本教程https://www.sammyk.me/upgrading-the-facebook-php-sdk-from-v4-to-v5 进行操作,到目前为止,我已成功将 SDK 集成到 codeignitor 等中

在我的控制器中我有

function facebook(){
    require_once __DIR__ . '/../vendor/autoload.php';

    $fb = new Facebook\Facebook([
      'app_id'     => 'xxx',
      'app_secret' => 'xxx',
      'default_graph_version' => 'v2.5',
    ]);

    $helper = $fb->getRedirectLoginHelper();
    $permissions = ['email', 'user_posts']; // optional
    $callback = 'http://localhost/project/api/index.php/social/facebook-callback';
    $loginUrl = $helper->getLoginUrl($callback, $permissions);
    echo $loginUrl;
}

这会为我生成一个 url。然后我点击这个链接,它把我带到 facebook 我点击确定然后它进入一个重定向页面。 在那个页面上我有

function facebook_callback(){
    require_once __DIR__ . '/../vendor/autoload.php';

    $fb = new Facebook\Facebook([
      'app_id'     => 'xxx',
      'app_secret' => 'xxx',
      'default_graph_version' => 'v2.5',
    ]);


$helper = $fb->getRedirectLoginHelper();
try {
    $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // There was an error communicating with Graph
  echo $e->getMessage();
  exit;
}

if (isset($accessToken)) {
  // User authenticated your app!
  // Save the access token to a session and redirect
  $_SESSION['facebook_access_token'] = (string) $accessToken;
  // Log them into your web framework here . . .
  // Redirect here . . .
  exit;
} elseif ($helper->getError()) {
  // The user denied the request
  // You could log this data . . .
  var_dump($helper->getError());
  var_dump($helper->getErrorCode());
  var_dump($helper->getErrorReason());
  var_dump($helper->getErrorDescription());
  // You could display a message to the user
  // being all like, "What? You don't like me?"
  exit;
}

    // If they've gotten this far, they shouldn't be here
    http_response_code(400);
    exit;
}

当我登陆页面时出现错误

跨站请求伪造验证失败。缺少必需的参数“状态”。

为什么会这样?这与会话有关吗?我已按照教程进行操作,但似乎没有任何效果。我发现的任何其他文档都尝试过,例如 session_start();Overwriting Persistent Data 但没用!

我们将不胜感激任何正确方向的观点。我用谷歌搜索了这个负载,没有明确的答案。

最好的问候

【问题讨论】:

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


    【解决方案1】:

    好的答案找到了!

    特别注意教程中的覆盖持久数据!所以这是一个与会话相关的问题。深夜这样做也是不明智的。

    我错过的是你必须从头开始这个过程,即facebook路由并获得一个全新的url链接,然后将该链接粘贴到url中,你将被重定向到facebook_callback。然后您将获得所需的数据。

    在这里回答,以防其他人陷入“到底发生了什么(深夜陷阱时)!”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多