简体   繁体   English

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

[英]How can I get campaign data from facebook access token?

Now I am developing some featured site. 现在,我正在开发一些特色网站。 Using the facebook API, I got facebook_access_token and stored it in database for use. 使用facebook API,我得到了facebook_access_token并将其存储在数据库中以供使用。 If I use access_token of the same facebook developer account, then successfully I got campaign datas. 如果我使用同一个Facebook开发者帐户的access_token,则可以成功获取广告系列数据。 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. 但是,如果我使用其他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

How can I get campaign data from facebook access token. 如何从Facebook访问令牌获取广告系列数据。

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 开发者帐户access_token和帐户ID为

"xxxxxxxxxxx"

and "xxxxxxxxxx". "xxxxxxxxxx". And the test account access_token and account id is "xxxxxxxxxxxx" and 并且测试帐户access_token和帐户ID为"xxxxxxxxxxxx"并且

"xxxxxxxxxxxxxxx".

If I can't get datas with this method, how can I get datas by access token? 如果无法使用此方法获取数据,如何通过访问令牌获取数据?

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: 要通过API访问Facebook Insights数据,需要执行以下两项操作:

  1. A dev token with the proper read_insights permissions 具有适当的read_insights权限的开发者令牌
  2. The account MUST be an admin 该帐户必须是管理员

You must give the 2nd account you mentioned admin access to the app or just use the initial account. 您必须将您提到的第二个帐户授予管理员对该应用程序的访问权限,或者仅使用初始帐户即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM