Reputation: 2113
I have a little issue getting access to some ad data via an app
I have these two scripts:
Login
<?php
include_once __DIR__ . '/includes/connect.php';
require_once __DIR__ . '/vendor/autoload.php';
use Facebook\Facebook;
session_start();
$facebook = new Facebook([
'app_id' => 'XXX',
'app_secret' => 'XXX',
'default_graph_version' => 'v2.5',
]);
$helper = $facebook->getRedirectLoginHelper();
$permissions = ['ads_read'];
$loginUrl = $helper->getLoginUrl('http://sparnorddashboard.local:8080/fb_callback.php', $permissions);
echo '<a href="'. $loginUrl .'">Log ind</a>';
?>
Callback
<?php
include_once __DIR__ . '/includes/connect.php';
require_once __DIR__ . '/vendor/autoload.php';
use Facebook\Facebook;
session_start();
$facebook = new Facebook([
'app_id' => 'XXX',
'app_secret' => 'XXX',
'default_graph_version' => 'v2.5',
]);
$helper = $facebook->getRedirectLoginHelper();
try {
$access_token = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo $e->getMessage();
exit;
}
if(!isset($access_token)) {
if($helper->getError()) {
header("HTTP/1.0 401 Unauthorized");
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
$oAuth2Client = $facebook->getOAuth2Client();
$tokenMetaData = $oAuth2Client->debugToken($access_token);
$tokenMetaData->validateAppId("1694068770828491");
$tokenMetaData->validateExpiration();
if(!$access_token->isLongLived()) {
try {
$access_token = $oAuth2Client->getLongLivedAccessToken($access_token);
} catch(Facebook\Exceptions\FacebookSDKException $e) {
$e->getMessage();
exit;
}
}
$_SESSION['fb_access_token'] = (string) $access_token;
header('Location: http://sparnorddashboard.local:8080/fetchads.php?callback=callback');
?>
But this will use the user client token - How can I set it up so that I can us the app access token? I don't want a user to login and accept permissions, as this is for a internal dashboard, that runs on a TV moniter, that will read ads data - nothing else.
Upvotes: 0
Views: 147
Reputation: 367
You can't use your APP access token for ads_read
. If you don't want to get it via user login, then another way is to get via Graph Api Explorer.
Assuming you already created an Ad Account :
Click 'Extended Permissions' tab and check ads_read
and click Get Access Token.
Use the user access token that will be presented to you in the access token form input field:
Use the generated access token for your internal dashboard app/script, e.g:
// Add to header of your file
use FacebookAds\Api;
// Initialize a new Session and instantiate an Api object
Api::init(
'{your-app-id}',
'{your-app-secret}',
$_SESSION['facebook_access_token'] // The generated access token
);
Upvotes: 1