【问题标题】:Facebook SDK returned an error: Failed to connect to graph.facebook.com port 443: Connection refusedFacebook SDK 返回错误:无法连接到 graph.facebook.com 端口 443:连接被拒绝
【发布时间】:2017-02-09 05:21:40
【问题描述】:

如何解决 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');

?>

【问题讨论】:

  • 你发布了你的秘密吗?尽快更改您的 app_secret。
  • 谢谢你能帮我解答这个问题

标签: php facebook facebook-graph-api


【解决方案1】:

您有 Facebook 应用程序吗?我不是说在你的手机上。您必须为您的 sdk 创建一个应用程序才能连接。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2018-10-15
    • 1970-01-01
    • 2019-07-29
    相关资源
    最近更新 更多