尝试通过 Facebook 营销 API 和 Python/Django 检索营销活动洞察数据时出现 FacebookRequestError

FacebookRequestError while trying to retrieve Campaign Insight Data via Facebook Marketing API with Python/Django

我正在尝试使用 Python Business SDK 通过 Facebook 的营销 API 获取营销活动洞察,但我收到 FacebookRequestError:

  Message: Call was not successful
  Method:  GET
  Path:    https://graph.facebook.com/v3.1/2603111949730990/insights
  Params:  {}

  Status:  400
  Response:
    {
      "error": {
        "message": "Error accessing adreport job.",
        "type": "OAuthException",
        "code": 2601,
        "error_subcode": 1815107,
        "is_transient": true,
        "error_user_title": "Loading Async Ads Report Failed",
        "error_user_msg": "Sorry, the report cannot be loaded successfully. Please check if your job status is completed instead of failed or running before fetching the data.",
        "fbtrace_id": "BQJsdi3g5tX"
      }
    }

我已经尝试修改 wait_for_async_job() 函数的代码,检查作业状态是否不是 'Job Completed' 并且作业完成百分比是否小于 100,但问题仍然存在。

def wait_for_async_job(async_job):
    async_job.remote_read()
    while async_job[AdReportRun.Field.async_status] != 'Job Completed' and async_job[AdReportRun.Field.async_percent_completion] < 100:
        time.sleep(1)
        async_job.remote_read()

如有任何帮助,我们将不胜感激。提前致谢!

我们已经解决了这个问题,问题出在 wait_for_async_job 中的 while 条件。应该有一个 'OR' 运算符而不是 'AND' 以便只要至少一个条件为真,循环就会迭代。这样,我们检查 async_status 应该是 'Job Completed' 并且完成百分比应该是 100。我把答案留在这里以防有人觉得它有帮助。

def wait_for_async_job(async_job):
    async_job.remote_read()
    while async_job[AdReportRun.Field.async_status] != 'Job Completed' or async_job[AdReportRun.Field.async_percent_completion] < 100:
        time.sleep(1)
        async_job.remote_read()