User Authorization Required when trying to download report with API call

Jump to solution
Community Member

I'm writing a Python script that runs a series of reports daily. So far, the script successfully runs the report, waits for it to finish, then stores the link to the file in a variable called fileURL. When I run the following request:

response = requests.get(fileURL, headers = headers)
I get the following error:

{"status":"unauthenticated","errors":[{"message":"user authorization required"}]}

I've included the access token as a parameter of the request header, so how can I authenticate this request?

Labels (1)
1 Solution
Community Member

Update in case anyone in the future comes across this:

Once the report is finished running (status = complete) there is a JSON value called "attachments" that you can get as a dictionary. Inside of the dictionary there is a value called "url" which returns a url to the file that you can download with a GET request. The relevant part of the code I used is below:

#The following chunk is ran in a recursive function - run status, check to see if statusID = complete - if so, continue, if not, sleep and then call the function again

#url = the HTTP url that I used to run the report

statusURL = url + "/" + str(id)
getStatus = requests.get(statusURL, headers = headers)
response = getStatus.json()
statusString = json.dumps(response)
parser = json.loads(statusString)
statusID = parser["status"]
#Once we get to here we know the report is completed - fileURL is the one you can call a GET request with
attachment = parser["attachment"]
fileURL = attachment["url"]

View solution in original post