如果是“此URL的域不包含在应用程序的域中”

时间:2018-11-14 12:32:56

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

我正在尝试从PHP脚本发布到Facebook墙。我创建了一个FB应用程序,安装了Graph PHP API,并实现了一些测试脚本,如下所示:

fbinit.php:

<?php

session_start();

require_once('src/Facebook/autoload.php');

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

?>

fbpost.php:

<?php

include('fbinit.php');

$helper = $fb->getRedirectLoginHelper();

$permissions = ['manage_pages','publish_pages']; //'publish_actions'
$loginUrl = $helper->getLoginUrl('https://www.REDACTED.net/fb-callback.php', $permissions);

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

?>

fb-callback.php:

<?php

include('fbinit.php');

$helper = $fb->getRedirectLoginHelper();
$_SESSION['FBRLH_state']=$_GET['state'];

try {

  $accessToken = $helper->getAccessToken();

} 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;

}

if (! isset($accessToken)) {
  echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.';
  exit;
}

try {

  $response = $fb->get('me/accounts', $accessToken->getValue());
  $response = $response->getDecodedBody();

} 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;

}

echo "<pre>";
print_r($response);
echo "</pre>";

?>

我第一次打开fbpost.php时,被要求按预期的那样登录Facebook,它要求允许我代为在页面上发帖,这很好。但是随后,我被重定向到了回调页面,并出现以下错误:

Graph returned an error: Can't load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and sub-domains of your app to the App Domains field in your app settings.

我添加了我能想到的应用程序URL和回调URL的每种组合,但是没有任何效果。有关应用设置的屏幕截图,请参见下文。应用程序ID和机密绝对正确。

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

redirect_uri参数的值必须与您的登录对话框调用以及随后尝试将代码交换令牌的API调用完全相同。

生成登录URL并处理响应后,响应会散布在不同的脚本中(即通过不同的URL调用),这很容易导致此类问题。如果将SDK留给自己的设备,则该SDK会尝试根据当前脚本URL找出该值。

在这种情况下,也请在getAccessToken方法调用中明确指定回调URL。