facebook登录图返回错误

时间:2018-04-28 10:07:45

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

login.php代码

if(!session_id()) {
    session_start();
}

require_once __DIR__ . '/vendor/autoload.php';

$facebook_appId = '123';
$facebook_appSecret = '123';

$fb = new Facebook\Facebook([
  'app_id' => $facebook_appId,
  'app_secret' => $facebook_appSecret,
  'default_graph_version' => 'v2.11',
]);

$helper = $fb->getRedirectLoginHelper();

$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://www.123.com/login-callback.php', $permissions);

echo '<a href="' . htmlspecialchars($loginUrl) . '">Login via Facebook!</a>';

login-callback.php代码

    // error_reporting (E_ALL & ~E_NOTICE);

if(!session_id()) {
    session_start();
}

require_once __DIR__ . '/vendor/autoload.php';

$facebook_appId = '123';
$facebook_appSecret = '123';

$fb = new \Facebook\Facebook([
    'app_id' => $facebook_appId,
    'app_secret' => $facebook_appSecret,
    'default_graph_version' => 'v2.11',
]);

// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
//   $helper = $fb->getRedirectLoginHelper();
//   $helper = $fb->getJavaScriptHelper();
//   $helper = $fb->getCanvasHelper();
//   $helper = $fb->getPageTabHelper();

try {
  // Get the \Facebook\GraphNodes\GraphUser object for the current user.
  // If you provided a 'default_access_token', the '{access-token}' is optional.

  $helper = $fb->getRedirectLoginHelper();  
  $accessToken = $helper->getAccessToken();
  $response = $fb->get('/me?fields=id,name', $accessToken );
  // $response = $fb->get('/me', $accessToken );

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

$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();

当我点击“login.php”输出链接时,它会跳转到“login-callback.php”并输出以下错误消息。我该如何解决这个问题?

图表返回错误:无法加载网址:这个URL的域名未包含应用的域名。为了能够加载URL,请在应用设置的应用域名字段添加应用的所有域和子域。

0 个答案:

没有答案