Skip to content

Instantly share code, notes, and snippets.

@thanhhh
Created November 17, 2015 08:23
Show Gist options
  • Save thanhhh/a0d79795978757a3b366 to your computer and use it in GitHub Desktop.
Save thanhhh/a0d79795978757a3b366 to your computer and use it in GitHub Desktop.
The callback page for get access token from Facebook
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$fbData = array(
'app_id' => '{APP ID}',
'app_secret' => '{APP SECRET}',
'default_graph_version' => 'v2.2'
);
$fb = new Facebook\Facebook($fbData);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
echo "Access Token: " . $accessToken;
} 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment