如何在 Facebook-PHP-SDK 中授权应用程序

How do I authorize a app in Facebook-PHP-SDK

对 PHP 非常陌生并下载了 Facebook-PHP-SDK,我正在尝试 运行 我在网上找到的一个简单代码,但我一直收到错误。我对这个问题做了很多研究,在这里查看了类似的问题等等,但没有找到任何有帮助的东西。看来我仍然需要授权我在 Facebook Developers 中创建的应用程序,但我不知道该怎么做。

<?php
require_once(__DIR__ . '/vendor/autoload.php');
$fb = new Facebook\Facebook([
  'app_id' => '{}',
  'app_secret' => '{}',
  'persistent_data_handler' => 'memory',
  'default_graph_version' => 'v3.3',
  ]);

$helper = $fb->getCanvasHelper();

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)) {
  echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.';
  exit;
}

// Logged in
echo '<h3>Signed Request</h3>';
var_dump($helper->getSignedRequest());

echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());

错误信息

No OAuth data could be obtained from the signed request. User has not authorized your app yet.

应用程序都在 https://developers.facebook.com/ 注册和管理。你在那里检查过你的设置吗?

您没有为 SDK 提供所需的 app_idapp_secret,因此它无法执行任何操作。这就是它给您 OAUTH 错误的原因。 (fb-docs)

$fb = new Facebook\Facebook([
  'app_id' => '{}', // Here
  'app_secret' => '{}', // Here
  'persistent_data_handler' => 'memory',
  'default_graph_version' => 'v3.3',
  ]);

您需要从 facebook app configuration dashboard 中获取该数据。并插入这个对象。

示例:

$app_id = "1234";
$app_secret = "foobar";
$fb = new Facebook\Facebook([
  'app_id' => '{$app_id}',
  'app_secret' => '{$app_secret}',
  'default_graph_version' => 'v3.2',
  ]);