$ accessToken返回null facebook oauth

时间:2018-03-17 21:51:02

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

我即将面临一个我无法在网上找到解决方案的问题,

这就是我的代码的样子

      session_start();
      require_once "Facebook/autoload.php";

$FB = new \Facebook\Facebook([
    'app_id' => '********',
    'app_secret' => '***********',
    'default_graph_version' => 'v2.10'
]);

$helper = $FB->getRedirectLoginHelper();
try {
    $accessToken = $helper->getAccessToken();
} catch (\Facebook\Exceptions\FacebookResponseException $e) {
    echo "Response Exception: " . $e->getMessage();
    exit();
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
    echo "SDK Exception: " . $e->getMessage();
    exit();
}

我已从此代码中调试了两个变量

var_dump($helper); // returns
    object(Facebook\Helpers\FacebookRedirectLoginHelper)#10 (4) { ["oAuth2Client":protected]=> object(Facebook\Authentication\OAuth2Client)#11 (4) { ["app":protected]=> object(Facebook\FacebookApp)#3 (2) { ["id":protected]=> string(15) "**********" ["secret":protected]=> string(32) "*************" } ["client":protected]=> object(Facebook\FacebookClient)#4 (2) { ["enableBetaMode":protected]=> bool(false) ["httpClientHandler":protected]=> object(Facebook\HttpClients\FacebookCurlHttpClient)#5 (4) { ["curlErrorMessage":protected]=> string(0) "" ["curlErrorCode":protected]=> int(0) ["rawResponse":protected]=> NULL ["facebookCurl":protected]=> object(Facebook\HttpClients\FacebookCurl)#6 (1) { ["curl":protected]=> NULL } } } ["graphVersion":protected]=> string(5) "v2.10" ["lastRequest":protected]=> NULL } ["urlDetectionHandler":protected]=> object(Facebook\Url\FacebookUrlDetectionHandler)#8 (0) { } ["persistentDataHandler":protected]=> object(Facebook\PersistentData\FacebookSessionPersistentDataHandler)#9 (1) { ["sessionPrefix":protected]=> string(6) "FBRLH_" } ["pseudoRandomStringGenerator":protected]=> object(Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator)#7 (0) { } }

和这个

   var_dump($accessToken); // return NULL

我不知道出了什么问题。

我正在使用version = v2.10和SDK 5

如何修复$ accessToken?

1 个答案:

答案 0 :(得分:-1)

如果$accessTokennull,则可能是用户拒绝了该请求。它在facebook docs中有详细记录。您应该尝试查看是否有任何来自帮助器$helper->getError()的转储。

# login-callback.php
$fb = new Facebook\Facebook([/* . . . */]);

$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)) {
  // Logged in!
  $_SESSION['facebook_access_token'] = (string) $accessToken;

  // Now you can redirect to another page and use the
  // access token from $_SESSION['facebook_access_token']
} elseif ($helper->getError()) {
  // The user denied the request
  exit;
}