Unsupported get request using Ads Facebook SDK for

2019-08-13 18:45发布

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

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

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];
}

It comes in the form:

access_token=xxxxxx

That's why I have to make an explode.

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

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

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]
Unsupported get request. Please read the Graph API documentation

Any suggestions?

UPDATE:

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

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

For these ones:

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

And now I get a different error message:

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

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.

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.

1条回答
Juvenile、少年°
2楼-- · 2019-08-13 19:19

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

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;
}
查看更多
登录 后发表回答