Facebook画布应用程序无法使用PHP SDK获取访问令牌

时间:2016-06-28 09:50:59

标签: php facebook canvas

好中午,

我正在尝试创建新的Facebook画布应用。 Facebook应用程序尝试从getCanvasHelper()方法获取访问令牌,但每次我尝试打印时都显示空白。我使用的是PHP SDK 5.0.0

$helper = $fb->getCanvasHelper();
$accessToken = $helper->getAccessToken();

以下是完整的示例代码。由于访问令牌问题,我无法进一步处理。

任何帮助都会很明显。

以下是我的示例代码。

<?php
session_start();

require_once __DIR__ . '/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
  'app_id' => 'my_app_id_i_have_replce_it_for_security',
  'app_secret' => 'my_app_secret_i_have_replce_it_for_security',
  'default_graph_version' => 'v2.4',
]);

$helper = $fb->getCanvasHelper();

$permissions = ['user_posts']; // optionnal

try {
    if (isset($_SESSION['facebook_access_token'])) {
    $accessToken = $_SESSION['facebook_access_token'];
    } else {
        $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 (isset($_SESSION['facebook_access_token'])) {
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    } else {
        $_SESSION['facebook_access_token'] = (string) $accessToken;

        // OAuth 2.0 client handler
        $oAuth2Client = $fb->getOAuth2Client();

        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);

        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;

        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }

    // validating the access token
    try {
        $request = $fb->get('/me');
    } catch(Facebook\Exceptions\FacebookResponseException $e) {
        // When Graph returns an error
        if ($e->getCode() == 190) {
            unset($_SESSION['facebook_access_token']);
            $helper = $fb->getRedirectLoginHelper();
            $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/bs-kapil-canvas/', $permissions);
            echo "<script>window.top.location.href='".$loginUrl."'</script>";
            exit;
        }
    } catch(Facebook\Exceptions\FacebookSDKException $e) {
        // When validation fails or other local issues
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }

    // getting all posts published by user
    try {
        $posts_request = $fb->get('/me/posts?limit=500');
    } 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;
    }

    $total_posts = array();
    $posts_response = $posts_request->getGraphEdge();
    if($fb->next($posts_response)) {
        $response_array = $posts_response->asArray();
        $total_posts = array_merge($total_posts, $response_array);
        while ($posts_response = $fb->next($posts_response)) {  
            $response_array = $posts_response->asArray();
            $total_posts = array_merge($total_posts, $response_array);  
        }
        print_r($total_posts);
    } else {
        $posts_response = $posts_request->getGraphEdge()->asArray();
        print_r($posts_response);
    }

    // Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
} else {
    $helper = $fb->getRedirectLoginHelper();
    $loginUrl = $helper->getLoginUrl('https://apps.facebook.com/bs-kapil-canvas/', $permissions);
    echo "<script>window.top.location.href='".$loginUrl."'</script>";
}

0 个答案:

没有答案