1

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!

aksonai
  • 101
  • 2
  • 7
  • My guess is to remove the first line in your function. And remove the check for percentage of job completion. Also, take the remote_read out of the loop. That's the statement that seems to be causing the error. Only run it after the job is completed. So the next line outside the while loop should be your remote_read. This is just my own guess. – Bobort Feb 14 '19 at 15:13
  • @Bobort thanks for the suggestion, but unfortunately it did not work. When I took out the remote_read out of the loop, the process was stuck in an infinite loop. In the debugger I saw that async_job[AdReportRun.Field.async_percent_completion] was 0 on every iteration. In the meantime I checked the job ID in Graph Explorer and the async job had a Finished status and percentage 100. So async_job.remote_read() must be doing the update of the percentage and when I take it out, there is an infinite loop. – aksonai Feb 14 '19 at 16:44
  • I see. I'm not too familiar with this product. Here's some documentation to help. You don't need to check the status, but keep the check on the percentage. https://developers.facebook.com/docs/marketing-api/insights/best-practices/#asynchronous – Bobort Feb 14 '19 at 17:58

1 Answers1

6

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()
aksonai
  • 101
  • 2
  • 7
  • I'm glad you got it solved, but I still don't think you need to check for "Job Completed" since you're already checking the percentage. Your answer, by changing `and` to `or` suggests that. – Bobort Feb 20 '19 at 17:13
  • Hey, thanks for checking back :) well in our logs there were cases when the job completion percentage was 100, but the status was "Job Running" and there was an interruption. After checking for both, the AdReportRuns seem to be working OK for now. – aksonai Feb 21 '19 at 14:49
  • Fascinating. I guess it makes sense. It might be at 100% and just finishing up a few minor things. Eh, I would never make an API in which 100% didn't mean job completed... – Bobort Feb 21 '19 at 23:06
  • 1
    Seems like `remote_read()` is getting deprecated. Replacing `async_job.remote_read()` with `async_job.api_get()` will work too. – sasawatc Jul 23 '19 at 20:49
  • 5
    this method doesn't cover the case where the Job Failed. In that case, the loop will run undefinitely – Missaratiskhona Oct 15 '19 at 12:17