尝试使用Python / Django通过Facebook Marketing API检索Campaign Insight数据时出现FacebookRequestError

时间:2019-02-14 11:42:22

标签: python django facebook-marketing-api facebook-python-business-sdk

我正在尝试使用Python Business SDK通过Facebook的Marketing API获得Campaign Insights,并且出现了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"
      }
    }

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

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

任何帮助将不胜感激。预先谢谢你!

1 个答案:

答案 0 :(得分:1)

我们已经解决了这个问题,问题在于wait_for_async_job中的while条件。只要有至少一个条件为True,就应该有一个“ 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()