现在我正在开发一些特色网站。 使用facebook API,我获得了facebook_access_token并将其存储在数据库中以供使用。 如果我使用相同的Facebook开发者帐户的access_token,那么我成功获得了广告系列数据。 但是如果我使用不同facebook帐户的access_token,那么我就无法获得包含指标的广告系列数据 - 喜欢,覆盖面等。 来自facebook SDK的错误消息如下。
This Ads API call requires the user to be admin of the application. User is not admin or developer of this application. in /ezond/networks/facebook/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Exception/RequestException.php:140
如何从Facebook访问令牌中获取广告系列数据。
下面是代码段。
<?php
require_once __DIR__ . '/vendor/autoload.php';
// Add to header of your file
use FacebookAds\Api;
use FacebookAds\Object\User;
use FacebookAds\Object\Campaign;
use Facebook\Facebook;
use Facebook\FacebookRequest;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
$facebookAppID = "456797558007270";
$facebookAppSecret = "c69f7f97677d5852875a23045305cc8e";
$facebook_access_token = "xxxxxxxxxxxxxxx";
$viewID = "xxxxxxxxxx";
if(isset($_GET['viewID'])) $viewID = $_GET['viewID'];
if($viewID == "") exit();
if(isset($_GET['refreshToken'])) $facebook_access_token = $_GET['refreshToken'];
if($facebook_access_token == "") exit();
$fb = new Facebook([
'app_id' => $facebookAppID,
'app_secret' => $facebookAppSecret,
'default_graph_version' => 'v2.9',
]);
// Initialize a new Session and instantiate an Api object
Api::init(
'456797558007270', // App ID
'c69f7f97677d5852875a23045305cc8e',
$facebook_access_token // Your user access token
);
$me = new User('me');
$my_ad_accounts = $me->getAdAccounts()->getObjects();
$campaign_fields = array(
'name',
'status',
'effective_status',
'objective'
);
$insight_fields = array(
'clicks',
'impressions',
'cpc',
'cpm',
'cpp',
'ctr',
'reach'
);
$insight_params = array(
'date_preset' => 'lifetime'
);
$ret = new stdClass();
foreach ($insight_fields as $insight_name) {
$ret->$insight_name = 0;
}
for ($i=0; $i < sizeof($my_ad_accounts); $i++) {
$my_ad_account = $my_ad_accounts[$i];
if($viewID == $my_ad_account->account_id){
try {
$account_campaigns = $my_ad_account->getCampaigns();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
exit;
}
$my_campaigns = $account_campaigns->getObjects();
if (sizeof($my_campaigns) >= 1){
$campaign = $my_campaigns[0];
$campaign = $campaign->getSelf($campaign_fields);
/*foreach ($campaign_fields as $field_name) {
echo $field_name . ': ' . $campaign->$field_name . '<br />';
}*/
$campaign_insights = $campaign->getInsights($insight_fields, $insight_params)->current();
if($campaign_insights){
foreach ($insight_fields as $insight_name) {
$ret->$insight_name += $campaign_insights->$insight_name;
}
} else {
// echo "NO";
}
}
}
}
foreach ($insight_fields as $insight_name) {
$ret->$insight_name = round($ret->$insight_name, 2);
}
echo json_encode($ret);
?>
开发者帐户access_token和帐户ID
"xxxxxxxxxxx"
和"xxxxxxxxxx".
并且测试帐户access_token和帐户ID是
"xxxxxxxxxxxx"
和
"xxxxxxxxxxxxxxx".
如果我无法使用此方法获取数据,我如何通过访问令牌获取数据?
答案 0 :(得分:1)
This Ads API call requires the user to be admin of the application. User is not admin or developer of this application.
要通过API访问Facebook Insights数据,需要两件事:
您必须将您提及的第二个帐户授予管理员访问权限,或者只使用初始帐户。