Facebook SDK返回错误:无法连接到graph.facebook.com端口443:连接被拒绝

时间:2017-02-09 05:21:40

标签: php facebook facebook-graph-api

如何在Facebook SDK中解决此错误 - > Facebook SDK返回错误:无法连接到graph.facebook.com端口443:连接被拒绝。我尝试了很多东西,但这个错误不会发生。以下是我的代码请你们帮助我。



<?php
session_start();
$fb = dirname(__FILE__) . '/../Facebook/facebook-sdk-v5/autoload.php';
require_once($fb); 

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

$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('1857969964448675'); // 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://example.com/members.php');

?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你有Facebook应用吗?我的电话并不意味着什么。您必须为您的sdk创建一个应用程序才能连接。

https://developers.facebook.com/apps创建一个应用。然后获取app-id和app-secret。

我敢打赌你会得到更多的错误,但那是开始。