【问题标题】:How can I get campaign data from facebook access token?如何从 facebook 访问令牌中获取活动数据?
【发布时间】:2017-08-15 03:05:33
【问题描述】:

现在我正在开发一些特色网站。 使用 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 访问令牌中获取活动数据。

代码sn-p如下。

<?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".

如果我无法通过此方法获取数据,如何通过访问令牌获取数据?

【问题讨论】:

  • 永远不要像那样公开暴露访问令牌。既然您现在已经在这里完成了,您现在需要使它们无效(除非您希望这里的其他任何人能够读取这些令牌并以这两个用户的名义执行范围允许的任何操作。)
  • 然后,更仔细地阅读营销 API 的文档 - 如果您处于开发阶段,那么 Facebook 会故意限制您的应用可以访问的帐户。
  • 嗨,CBroe。谢谢你的评论。如果我完成了应用开发,那我需要做什么?

标签: php facebook facebook-graph-api facebook-access-token


【解决方案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 数据需要做两件事:

  1. 具有适当 read_insights 权限的开发令牌
  2. 帐户必须是管理员

您必须授予您提到的第二个帐户对应用的管理员访问权限,或者只使用初始帐户。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2016-08-26
    • 1970-01-01
    • 2021-03-23
    相关资源
    最近更新 更多