Last active
December 19, 2015 05:50
-
-
Save thanhhh/208f82bd872a01b71bb4 to your computer and use it in GitHub Desktop.
Get page access token from Facebook page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__ . '/vendor/autoload.php'; | |
use Facebook\Authentication\AccessToken; | |
use Facebook\FacebookApp; | |
use Facebook\FacebookRequest; | |
session_start(); | |
$app_id = '{APP ID}'; | |
$app_secret = '{APP SECRET}'; | |
$app = new FacebookApp($app_id, $app_secret); | |
$fb = new Facebook\Facebook(array( | |
'app_id' => $app_id, | |
'app_secret' => $app_secret, | |
'default_graph_version' => 'v2.5', | |
)); | |
$page_id = '{PAGE ID}'; | |
//User access token get from cakllback.php | |
$access_token = new AccessToken('{USER ACCESS TOKEN}'); | |
$request = new FacebookRequest( | |
$app, | |
$access_token, | |
'GET', '/' . $page_id, | |
array( 'fields' => 'access_token') ); | |
// Send the request to Graph | |
try { | |
$response = $fb->getClient()->sendRequest($request); | |
$graphNode = $response->getGraphNode(); | |
echo 'Page Access Token: ' . $graphNode['access_token']; | |
} 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