通过应用
访问某些广告数据时遇到一些问题我有这两个脚本:
登录
<?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>';
?>
回调
<?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');
?>
但是这将使用用户客户端令牌 - 如何设置它以便我可以使用应用程序访问令牌?我不希望用户登录并接受权限,因为这是针对在电视监听器上运行的内部仪表板,它将读取广告数据 - 没有别的。
答案 0 :(得分:1)
您无法使用ads_read
的APP访问令牌。如果您不想通过用户登录获取,那么另一种方法是通过Graph Api Explorer获取。
假设您已经创建了Ad Account :
将生成的访问令牌用于内部仪表板应用程序/脚本,例如:
// 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
);