Unsupported get request using Ads Facebook SDK for PHP

☆樱花仙子☆ 提交于 2019-12-24 16:54:21

问题


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:


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


来源:https://stackoverflow.com/questions/31428476/unsupported-get-request-using-ads-facebook-sdk-for-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!