Facebook 营销 Api - 运行 循环抛出 "AuthorizationException An unknown error has occurred."(第二次)

Facebook Marketing Api - running on loop throws "AuthorizationException An unknown error has occurred." (on 2nd time)

我正在使用 Facebook 广告 SDK 并尝试将用户添加到自定义受众列表。因为有 10000 data limit at a time,为了将来的证明,我正在尝试循环添加数据。

但是,当它第二次命中时,它会抛出错误。

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

如果 addUsersMultiKey() 运行 1 次,它运行良好,但是如果运行 2 次,它会抛出错误

FacebookAds\Http\Exception\AuthorizationException - An unknown error has occurred.


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。我尝试在 test() 函数之外循环,并将 App::init() 保留在 test() 函数内,因此 App::init() 被多次调用。还是一样的问题。


This answers 说要单独执行请求,但怎么可能这样做呢?但是,我不是尝试了所有可能的场景吗?

The error seems to be with batched requests - The solution for us was to change the code to do the requests individually.


我认为这与运行时有关,因为当我刷新时,它工作正常;所以我不认为这是节流问题(我也尝试过sleep(10),事实上它在刷新时有效)。

我该如何解决这个问题?


堆栈跟踪:

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

更新: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);

这里,响应数组有两个请求。第一个成功,第二个同样的问题。

$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"}}"

我已将其作为评论发布,但格式没有帮助。

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

这样您就可以检查所提出的请求的实际内容。如果请求相同,那肯定是facebook的bug。

编辑:放在上面的确切代码 $raw_response

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

我发现了这个错误,它实际上是我这边的。

当我分块时,我在块中的索引保留了它们实际数组的索引,并且由于它没有给出 [0,1,2],这导致 api 调用失败。

array_values($chunk); 应该可以解决这个问题。

对于 Laravel 的 collection $chunk->values()->toArray();