Tom
Tom

Reputation: 145

Difficulty authorizing app in Facebook-PHP-SDK

I am struggling to authorize my app in Facebook-PHP-SDK. The sample code I got form this link, https://developers.facebook.com/docs/php/howto/example_access_token_from_canvas, is a simple piece of code to get an access token but I cannot get my access token to access any data from Facebook through my code because I do not know how to authorize it. One problem might be I have not finished the setup of Facebook Login as seen in the screenshot below. Could that be what is causing the problem? Code and error message below too. Note: I have omitted my app id and secret for security reasons.

App

PHP Code

<?php
require_once(__DIR__ . '/vendor/autoload.php');
$app_id = "";
$app_secret ="";
$fb = new Facebook\Facebook([
        'app_id' => '{$app_id}',
        'app_secret' => '{$app_secret}',
        'persistent_data_handler' => 'memory',
        'default_graph_version' => 'v3.3',
]);

$helper = $fb->getCanvasHelper();
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;
}

        echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.';
        exit;
}
// Logged in
echo '<h3>Signed Request</h3>';
var_dump($helper->getSignedRequest());

echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());

Error Message

No OAuth data could be obtained from the signed request. User has not authorized your app yet.

Upvotes: 0

Views: 51

Answers (1)

Soufiane Lamnizeh
Soufiane Lamnizeh

Reputation: 434

You must login and authorize your application before to get the token, you must have a login page and the redirect page to fetch the token. check this link, because you must start by this: example login FB

Have Fun :)

Upvotes: 1

Related Questions