如何从Facebook访问令牌获取广告系列数据?

问题描述:

现在我正在开发一些特色网站。 使用facebook API,我得到了facebook_access_token并将其存储在数据库中供使用。 如果我使用同一个Facebook开发者帐户的access_token,那么我就可以成功获得广告系列数据。 但是,如果我使用不同的Facebook帐户的access_token,那么我无法获得包含指标的广告系列数据 - 喜欢,覆盖等。 并且来自Facebook SDK的错误消息如下。如何从Facebook访问令牌获取广告系列数据?

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

永远不要公开公开这样的访问令牌。既然你现在已经在这里做了,你现在需要使它们失效(除非你想让这里的其他读者能够获取这些标记并执行范围允许以这两个用户的名义进行的任何操作)。 – CBroe

+0

然后,请仔细阅读营销API的文档 - 如果您处于开发级别,则Facebook有意限制您的应用可访问的帐户。 – CBroe

+0

嗨,CBroe。谢谢你的评论。如果我完成了应用程序开发,那么我需要做什么? –

This Ads API call requires the user to be admin of the application. User is not admin or developer of this application. 

要通过API访问Facebook洞察数据,需要两两件事:

  1. 用正确的read_insights权限的dev的令牌
  2. 的帐户必须是管理员

你必须给你提到的第二个帐户管理员访问应用程序或只使用初始帐户。