简体   繁体   English

Facebook Graph API PHP SDK 5.0-getLoginUrl()出现500错误

[英]Facebook Graph API PHP SDK 5.0 - 500 Error on getLoginUrl()

I am building a WordPress plugin that connects to the Facebook Graph API and will eventually be used to post to Facebook pages. 我正在构建一个WordPress插件,该插件可连接到Facebook Graph API,并将最终用于发布到Facebook页面。 I have followed their docs and I am getting a 500 server error when I click on "Log In With Facebook" The code="token" comes back in the url however there is a 500 server error. 我关注了他们的文档,当我单击“使用Facebook登录”时,我收到500服务器错误。URL中返回了code =“ token”,但是有500服务器错误。

I am running.. 我在跑步..

  • PHP: 5.4 的PHP:5.4
  • Mbstring is enabled Mbstring已启用

If I take everything out of the fb-callback.php file and just echo 'working' it shows on the page and nothing breaks, but when I just have the facebook connect below it breaks... and I get the 500 error. 如果我从fb-callback.php文件中取出所有内容并仅回显“工作”,它就会显示在页面上,并且没有任何中断,但是当我只有下面的Facebook连接时,它就会中断...并且出现500错误。

$fb = new Facebook\Facebook([
  'app_id' => 'my-secret',
  'app_secret' => 'my-id',
  'default_graph_version' => 'v2.8',
  ]);

Here is the rest of the code. 这是其余的代码。 Keep in mind this is a WordPress Plugin and I it is my first one I am developing so I am sure it is a bit chaotic in its organization. 请记住,这是一个WordPress插件,这是我正在开发的第一个插件,因此我确信它的组织结构有些混乱。

Settings Page: 设置页面:

<?php
    session_start();
    $fb = new Facebook\Facebook([
      'app_id' => 'my-id',
      'app_secret' => 'my-secret',
      'default_graph_version' => 'v2.8',
    ]);

    $helper = $fb->getRedirectLoginHelper();

    $permissions = ['email', 'manage_pages', 'publish_pages', 'user_posts']; // Optional permissions

    $loginUrl = $helper->getLoginUrl('http://p11socialplugin.p11.com/wp-content/plugins/p11-social/fb-callback.php', $permissions);

    echo '<a href="' . htmlspecialchars($loginUrl) . '">Log in with Facebook!</a>';

    ?>

Here is the entire fb-callback.php 这是整个fb-callback.php

<?php
$fb = new Facebook\Facebook([
  'app_id' => 'my-app-id ', // Replace {app-id} with your app id
  'app_secret' => 'my-app-secret',
  'default_graph_version' => 'v2.8',
  ]);

$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}); // Replace {app-id} with your 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;

// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
//header('Location: https://p11socialplugin.p11.com/wp-admin');

?>

Error Log 错误记录

[30-Jan-2017 22:03:28 UTC] PHP Warning:  session_start(): Cannot send session cookie - headers already sent by (output started at /Users/garrett/Development/wp-playground/wp-content/themes/twentyseventeen/header.php:16) in /Users/garrett/Development/wp-playground/wp-content/themes/twentyseventeen/facebook.php on line 2
    [30-Jan-2017 22:03:28 UTC] PHP Warning:  session_start(): Cannot send session cache limiter - headers already sent (output started at /Users/garrett/Development/wp-playground/wp-content/themes/twentyseventeen/header.php:16) in /Users/garrett/Development/wp-playground/wp-content/themes/twentyseventeen/facebook.php on line 2
    [30-Jan-2017 22:03:58 UTC] PHP Warning:  session_start(): Cannot send session cookie - headers already sent by (output started at /Users/garrett/Development/wp-playground/wp-admin/includes/template.php:1995) in /Users/garrett/Development/wp-playground/wp-content/plugins/p11-social/class.p11-social.php on line 103
    [30-Jan-2017 22:03:58 UTC] PHP Warning:  session_start(): Cannot send session cache limiter - headers already sent (output started at /Users/garrett/Development/wp-playground/wp-admin/includes/template.php:1995) in /Users/garrett/Development/wp-playground/wp-content/plugins/p11-social/class.p11-social.php on line 103
    [30-Jan-2017 22:04:08 UTC] PHP Warning:  session_start(): Cannot send session cookie - headers already sent by (output started at /Users/garrett/Development/wp-playground/wp-admin/includes/template.php:1995) in /Users/garrett/Development/wp-playground/wp-content/plugins/p11-social/class.p11-social.php on line 103
    [30-Jan-2017 22:04:08 UTC] PHP Warning:  session_start(): Cannot send session cache limiter - headers already sent (output started at /Users/garrett/Development/wp-playground/wp-admin/includes/template.php:1995) in /Users/garrett/Development/wp-playground/wp-content/plugins/p11-social/class.p11-social.php on line 103

You need to call session_start() at the beginning of fb-callback.php as well. 您还需要在fb-callback.php的开头调用session_start()

And you will also need to pass an additional argument to the Facebook\\Facebook constructor, 'persistent_data_handler' => 'session' , otherwise you will get CSRF exception. 而且,您还需要将其他参数传递给Facebook \\ Facebook构造函数'persistent_data_handler' => 'session' ,否则您将获得CSRF异常。

new Facebook([
        'app_id' => '{app-id}',
        'app_secret' => '{app-secret-key}',
        'default_graph_version' => 'v2.8',
        'persistent_data_handler'=> 'session'
    ]);

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

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