Reputation: 165
Now I am developing some featured site. Using the facebook API, I got facebook_access_token and stored it in database for use. If I use access_token of the same facebook developer account, then successfully I got campaign datas. But if I use the access_token of different facebook account, then I can't get campaign datas with metrics - likes, reach, etc. And the error message from facebook SDK is below.
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
How can I get campaign data from facebook access token.
The code snippet is below.
<?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);
?>
The developer account access_token and account id is
"xxxxxxxxxxx"
and "xxxxxxxxxx".
And the test account access_token and account id is
"xxxxxxxxxxxx"
and
"xxxxxxxxxxxxxxx".
If I can't get datas with this method, how can I get datas by access token?
Upvotes: 0
Views: 633
Reputation: 570
This Ads API call requires the user to be admin of the application. User is not admin or developer of this application.
To access Facebook Insights data via the API requires 2 things:
You must give the 2nd account you mentioned admin access to the app or just use the initial account.
Upvotes: 1