php - Facebook Marketing Api - running on loop throws "AuthorizationException An unknown error has occurred." (on 2nd time)
问题描述
I'm using Facebook Ads SDK and trying to add users to a custom audience list. As there is a 10000 data limit at a time, for future proof I am trying to add data in a loop.
However, when it hits the second time, it throws an error.
1.
public function test($audienceId, $data)
Api::init($appId, $appSecret, $token);
$audience = new CustomAudience($audienceId);
$schema = [
CustomAudienceMultikeySchemaFields::EMAIL,
CustomAudienceMultikeySchemaFields::PHONE,
//...
];
foreach (array_chunk($data, 500) as $chunk) {
$audience->addUsersMultiKey($chunk, $schema, true);
}
}
// And run as
$class = new FbTestClass();
$class->test($audienceId, $data);
If the addUsersMultiKey()
runs 1 time, it works perfectly, however if it runs 2 times, it throws an error
FacebookAds\Http\Exception\AuthorizationException - An unknown error has occurred.
2 Then I tried to move App:init()
part to __construct()
, but the issue persists.
public function __construct() {
Api::init($appId, $appSecret, $token);
}
public function test($audienceId) {
$audience = new CustomAudience($audienceId);
$schema = [
CustomAudienceMultikeySchemaFields::EMAIL, //...
];
$audience->addUsersMultiKey($chunk, $schema, true);
}
// And run as:
$class = new FbTestClass();
foreach (array_chunk($data, 500) as $chunk) {
$class->test($audienceId, $chunk);
}
3. I tried looping outside of the test()
function, and keeping App::init()
inside the test()
function, so App::init()
gets called multiple times. Still same issue.
This answers says to do the requests individually but how is it possible to do that? But, didn't I try all possible scenarios?
The error seems to be with batched requests - The solution for us was to change the code to do the requests individually.
I believe it's about the runtime because when I refresh, it works alright; so I don't think it's throttling issue (I also tried to sleep(10)
, in fact it works on refresh).
How can I overcome this issue?
Maybe there is a way to destroy
App
instance? Or is there a way to create a newApp
instance to overwrite previous one?I saw a batch request in the facebook docs but couldn't figure out how to use it with my use-case (custom audience & addUsersMultiKey()).
Stack Trace:
An unknown error has occurred. {"userId":1,"exception":"[object] (FacebookAds\\Http\\Exception\\AuthorizationException(code: 1): An unknown error has occurred. at /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Exception/RequestException.php:165)
[stacktrace]
#0 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Client.php(215): FacebookAds\\Http\\Exception\\RequestException::create()
#1 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Http/Request.php(286): FacebookAds\\Http\\Client->sendRequest()
#2 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(165): FacebookAds\\Http\\Request->execute()
#3 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Api.php(214): FacebookAds\\Api->executeRequest()
#4 /home/vagrant/Code/myApp/vendor/facebook/php-ads-sdk/src/FacebookAds/Object/CustomAudience.php(537): FacebookAds\\Api->call()
#5 is where I execute the code:
$audience->addUsersMultiKey($chunk, $schema, true);
Update: 4 I also tried same thing with graph-sdk
$fb = new Facebook([
'app_id' => env("FB_APP_ID"),
'app_secret' => env("FB_APP_SECRET"),
'default_graph_version' => 'v6.0',
'default_access_token' => env("FB_ACCESS_TOKEN")
]);
foreach ($chunks as $chunk) {
$payload = [
'schema' => $schema,
'data' => $chunk->toArray()
];
$requests[] = $fb->request("POST", "/$customAudienceId/users", ['payload' => $payload]);
}
$batchResponse = $fb->sendBatchRequest($requests);
Here, response array has both request. 1st one is successful, second one same issue.
$batchResponse contains:
responses: array:2 [▼
0 => Facebook\FacebookResponse ▼
body => // successful data returned...
1 => Facebook\FacebookResponse ▼
body => "{"error":{"message":"An unknown error has occurred.","type":"OAuthException","code":1,"fbtrace_id":"A5...464cGc22gi28Kb"}}"
解决方案
我发现了这个错误,它实际上是我的结果。
当我进行分块时,我的块索引保留了实际数组的索引,并且由于它没有给出 [0,1,2],因此导致 api 调用失败。
array_values($chunk);
应该解决这个问题。
对于 Laravel 的收藏$chunk->values()->toArray();
推荐阅读
- rust - 引用不存在的 d: 驱动器的 Windows 上的 Rust 编译
- cordova - 切换到 Cordova iOS 6.1.0 后出现白屏死机
- python - 为什么人们不能连接到这个用 python 套接字制作的“服务器”?
- laravel - 电子邮件验证后 Laravel 重定向到密码重置
- php - 如何从mysql加载和显示数据?
- debian - 无法解析 .onion 域
- xcode - onAppear 在空白项目中被调用两次的问题
- python - Flask AttributeError:“NoneType”对象没有属性“名称”
- c# - 如何将@html.editorfor 居中?
- javascript - 纱线因 TypeError 失败:无法读取属性 'yarnPath' of null