简体   繁体   中英

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

I am trying to get Campaign Insights via Facebook's Marketing API using the Python Business SDK and I am getting a 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"
      }
    }

I already tried to modify the code for the wait_for_async_job() function by checking if the job status is not 'Job Completed' and the percentage of job completion is smaller than 100 but the issue persists.

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

Any help would be much appreciated. Thank you in advance!

We already solved this, the issue was with the while condition in the wait_for_async_job. There should be an 'OR' operator and not 'AND' so that the loop is iterating as long as at least one of the conditions is True. This way, we check that both async_status should be 'Job Completed' and the completion percentage should be 100. I am leaving the answer here in case anyone finds it helpful.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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