PHP 不支持使用 Ads Facebook SDK 获取请求
Unsupported get request using Ads Facebook SDK for PHP
我正在尝试使用 Facebook php-ads-sdk
访问与广告活动相关的数据(例如国家/地区、每千次展示费用、花费...)
Facebook 用户已在 Facebook Bussiness Manager 中添加为管理员。
我做的第一件事是根据 app_secret 和 app_id 获取访问令牌用户:
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
这就是为什么我要爆炸的原因。
获得访问令牌后,我在 Api:
中进行身份验证
Api::init($this->appId, $this->appSecret, $this->accessToken);
$api = Api::instance();
最后,我尝试使用 Bussiness Manager 帐户 ID 获取一些数据:
$account = new AdAccount($this->accountId);
$test = $account->getAdCampaigns();
但我收到以下错误消息:
[FacebookAds\Http\Exception\AuthorizationException]
Unsupported get request. Please read the Graph API documentation
有什么建议吗?
更新:
按照 Paul Bain 在第一条评论中的建议,我更改了这些行:
$account = new AdAccount($this->accountId);
$test = $account->getAdCampaigns();
对于这些:
$account = new AdAccount('act_' . $this->accountId);
$test = $account->getAdCampaigns();
现在我收到一条不同的错误消息:
(#10) You do not have sufficient permissions to perform this action
我不知道为什么我没有足够的权限,因为正如我之前所说,用户在 Facebook Bussiness Manager 中被添加为管理员。
同样在 facebook 用户配置文件中,在 settings/advanced 下,Facebook Bussiness Manager 帐户 ID 添加到 'Authorized Ad Account IDs'
该应用已获准基本访问级别。
您需要在您尝试访问的 adaccount 的 ID 前加上 act_
前缀,否则这将不起作用。
例如,如果您执行以下操作,您将在 API 响应中看到所有 adaccounts 的 ID 都以这种方式作为前缀:
use FacebookAds\Object\AdUser;
$user = new AdUser('me');
$accounts = $user->getAdAccounts(array('name'));
foreach($accounts as $act) {
echo $account->id." - ".$account->name.PHP_EOL;
}
我正在尝试使用 Facebook php-ads-sdk
访问与广告活动相关的数据(例如国家/地区、每千次展示费用、花费...)Facebook 用户已在 Facebook Bussiness Manager 中添加为管理员。
我做的第一件事是根据 app_secret 和 app_id 获取访问令牌用户:
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
这就是为什么我要爆炸的原因。
获得访问令牌后,我在 Api:
中进行身份验证Api::init($this->appId, $this->appSecret, $this->accessToken);
$api = Api::instance();
最后,我尝试使用 Bussiness Manager 帐户 ID 获取一些数据:
$account = new AdAccount($this->accountId);
$test = $account->getAdCampaigns();
但我收到以下错误消息:
[FacebookAds\Http\Exception\AuthorizationException]
Unsupported get request. Please read the Graph API documentation
有什么建议吗?
更新:
按照 Paul Bain 在第一条评论中的建议,我更改了这些行:
$account = new AdAccount($this->accountId);
$test = $account->getAdCampaigns();
对于这些:
$account = new AdAccount('act_' . $this->accountId);
$test = $account->getAdCampaigns();
现在我收到一条不同的错误消息:
(#10) You do not have sufficient permissions to perform this action
我不知道为什么我没有足够的权限,因为正如我之前所说,用户在 Facebook Bussiness Manager 中被添加为管理员。
同样在 facebook 用户配置文件中,在 settings/advanced 下,Facebook Bussiness Manager 帐户 ID 添加到 'Authorized Ad Account IDs'
该应用已获准基本访问级别。
您需要在您尝试访问的 adaccount 的 ID 前加上 act_
前缀,否则这将不起作用。
例如,如果您执行以下操作,您将在 API 响应中看到所有 adaccounts 的 ID 都以这种方式作为前缀:
use FacebookAds\Object\AdUser;
$user = new AdUser('me');
$accounts = $user->getAdAccounts(array('name'));
foreach($accounts as $act) {
echo $account->id." - ".$account->name.PHP_EOL;
}