不支持使用Ads Facebook SDK for PHP获取请求 [英] Unsupported get request using Ads Facebook SDK for PHP

查看:143
本文介绍了不支持使用Ads Facebook SDK for PHP获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Facebook php-ads-sdk

I'm trying to access ads campaigns related data (such as country, cpm, spent ...) using Facebook php-ads-sdk

已经在Facebook Bussiness Manager中将Facebook用户添加为管理员.

The Facebook user is already added as admin in Facebook Bussiness Manager.

我要做的第一件事是根据用户的 app_secret app_id 获得访问令牌:

The first thing I do is get the access token given the app_secret and app_id of the user:

public function getAccessToken()
{
    try {
        $response = $this->client->get(
            $this->accessTokenUrl, [
            'query' => [
                'client_id' => $this->appId,
                'client_secret' => $this->appSecret,
                'grant_type' => 'client_credentials',
            ],
            'stream' => true
        ]);
    } catch(RequestException $e) {
        echo $e->getMessage();
    }

    $body = $response->getBody();
    $contents = $body->getContents();

    $contentsArray = explode('=', $contents);

    return $contentsArray[1];
}

它的形式为:

access_token = xxxxxx

access_token=xxxxxx

这就是为什么我必须爆炸.

That's why I have to make an explode.

获得访问令牌后,我将在Api中进行身份验证:

Once I got the access token, I authenticate in the Api:

Api::init($this->appId, $this->appSecret, $this->accessToken);
$api = Api::instance();

最后,我尝试使用Bussiness Manager帐户ID获取一些数据:

Finally I try to get some data using the Bussiness Manager account id:

$account = new AdAccount($this->accountId);
$test = $account->getAdCampaigns();

但是我收到以下错误消息:

But I get the following error message:

[FacebookAds \ Http \ Exception \ AuthorizationException]
不支持的获取请求.请阅读Graph API文档

[FacebookAds\Http\Exception\AuthorizationException]
Unsupported get request. Please read the Graph API documentation

有什么建议吗?

更新:

正如保罗·贝恩(Paul Bain)在第一条评论中所建议的那样,我更改了以下几行:

As suggested in the first comment by Paul Bain, I changed these lines:

$account = new AdAccount($this->accountId);
$test = $account->getAdCampaigns();

对于这些:

$account = new AdAccount('act_' . $this->accountId);
$test = $account->getAdCampaigns();

现在我得到了另一条错误消息:

And now I get a different error message:

(#10)您没有足够的权限执行此操作

(#10) You do not have sufficient permissions to perform this action

我不知道为什么我没有足够的权限,因为正如我之前所说,在Facebook Bussiness Manager中将用户添加为管理员之前.

I don't know why I don't have sufficient permissions, since as I said before the user is added as admin in Facebook Bussiness Manager.

还在Facebook用户个人资料的设置/高级"下,在授权广告帐户ID"中添加了Facebook Bussiness Manager帐户ID

Also in the facebook user profile, under settings/advanced, the Facebook Bussiness Manager account id is added in 'Authorized Ad Account IDs'

该应用已获准使用基本访问权限级别.

The app is approved for Basic access level.

推荐答案

您需要为要尝试访问的adaccount的ID加上act_前缀,否则将无法正常工作.

You need to prefix the ID of the adaccount you are trying to access with act_ otherwise this will not work.

例如,如果执行以下操作,则会在API响应中看到所有adaccounts的ID都以这种方式加上前缀:

For example, if you do the following, you will see that the IDs of all the adaccounts are prefixed this way in the API response:

use FacebookAds\Object\AdUser;
$user = new AdUser('me');
$accounts = $user->getAdAccounts(array('name'));
foreach($accounts as $act) {
  echo $account->id." - ".$account->name.PHP_EOL;
}

这篇关于不支持使用Ads Facebook SDK for PHP获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆