简体   繁体   English

Facebook 营销 Api - 在循环上运行抛出“AuthorizationException 发生未知错误”。 (第二次)

[英]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.我正在使用 Facebook 广告 SDK 并尝试将用户添加到自定义受众列表。 As there is a 10000 data limit at a time , for future proof I am trying to add data in a loop.由于一次有10000 个数据限制,为了将来的证明,我正在尝试在循环中添加数据。

However, when it hits the second time, it throws an error.但是,当它第二次命中时,它会引发错误。

1. 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如果addUsersMultiKey()运行 1 次,它可以完美运行,但是如果它运行 2 次,则会引发错误

FacebookAds\Http\Exception\AuthorizationException - An unknown error has occurred. FacebookAds\Http\Exception\AuthorizationException - 发生未知错误。


2 Then I tried to move App:init() part to __construct() , but the issue persists. 2 然后我尝试将App:init()部分移动到__construct() ,但问题仍然存在。

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. 3. 我尝试在test() function 之外循环,并将App::init()保持在test() function 内,因此App::init()被多次调用。 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).所以我认为这不是节流问题(我也尝试过sleep(10) ,实际上它在刷新时有效)。

How can I overcome this issue?我该如何克服这个问题?

  • Maybe there is a way to destroy App instance?也许有办法销毁App实例? Or is there a way to create a new App instance to overwrite previous one?或者有没有办法创建一个新的App实例来覆盖以前的?

  • 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()).在 facebook 文档中看到了一个批处理请求,但不知道如何在我的用例中使用它(自定义受众和 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更新:4 我也用 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"}}"

I found the bug, it was actually from my end.我发现了这个错误,它实际上是我的结果。

When I was chunking, my indexes in chunks were preserving their indexes of actual array, and as it wasn't giving [0,1,2], it was causing the api call to fail.当我进行分块时,我的块索引保留了实际数组的索引,并且由于它没有给出 [0,1,2],因此导致 api 调用失败。

array_values($chunk); should fix that issue.应该解决这个问题。

for Laravel's collection $chunk->values()->toArray();对于 Laravel 的集合$chunk->values()->toArray();

I've posted this as a comment, but the formatting didn't help.我已将此作为评论发布,但格式没有帮助。

Add following lines to FacebookAds/Http/Adapter/CurlAdapter.php just before $raw_response = $this->getCurl()->exec();将以下行添加到FacebookAds/Http/Adapter/CurlAdapter.php之前$raw_response = $this->getCurl()->exec(); . .

$fp = fopen("/temp/log_" . uniqid() . ".txt", "w"); // change this to any file, but make sure it changes between requests
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);

This way you can check the actual contents of the request being made.通过这种方式,您可以检查所发出请求的实际内容。 If the requests are the same, then it's definitely a facebook bug.如果请求相同,那么肯定是 facebook 错误。

Edit: Exact code to put above $raw_response编辑:放在$raw_response上面的确切代码

curl_setopt($this->getCurl()->getHandle(), CURLOPT_VERBOSE, 1);
curl_setopt($this->getCurl()->getHandle(), CURLOPT_STDERR, $fp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM