Facebook PHP SDK getAccessToken始终返回空白

时间:2015-10-21 04:28:55

标签: php facebook facebook-sdk-4.0

我使用的是Facebook SDK提供的代码,但它不起作用,因为它始终返回空白以进行accessstoken。我已经设置了正确的应用ID和密钥。

但它确实给了我[FBRLH_state] => 8013d8cb8ebc0fc5af09fd141dd13b1e,但我还不了解。

<?php

session_start();
require_once("functions.php");

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

use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;

// Init PHP Sessions
session_start();

try {
    $fb = new Facebook([
      'app_id' => FB_APP_ID,
      'app_secret' => FB_APP_SECRET
    ]);

} catch (Exception $e) {
    $message = 'Caught new Facebook: '.$e->getMessage();
    $result2=0;
}
echo "<pre>";print_r($message);echo "</pre>";


if (!isset($_SESSION['facebook_access_token'])) {
    $_SESSION['facebook_access_token'] = null;
}

if (!$_SESSION['facebook_access_token']) {
    echo "not facebook_access_token <br />";
    try {   
        $helper = $fb->getRedirectLoginHelper();
    } catch (Exception $e) {
        $message = 'Caught getRedirectLoginHelper2: '.$e->getMessage();
        $result2=0;
    }

    try {
        echo "try facebook_access_token <br />";
        $_SESSION['facebook_access_token'] = (string) $helper->getAccessToken();

    } catch(FacebookResponseException $e) {
        // When Graph returns an error
        echo 'Graph returned an error: ' . $e->getMessage();
        exit;
    } catch(FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }
}

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

if ($_SESSION['facebook_access_token']) {
    echo "You are logged in!";
} else {
    $permissions = ['ads_management'];
    $loginUrl = $helper->getLoginUrl('http://xxxx/vendor/', $permissions);
    echo '<a href="' . $loginUrl . '">Log in with Facebook</a>';
} 

?>

0 个答案:

没有答案