I’m using the following code to upload through the API:
# And ask Discourse where to send it.
r = requests.post(
f"https://{DISCOURSE}/uploads/generate-presigned-put", json=file_info, headers=HEADERS)
if r.status_code != 200:
print(
f"Error asking where to upload the image: got {r.status_code}", file=sys.stderr)
sys.exit(1)
upload_url = r.json()['url']
upload_uid = r.json()['unique_identifier']
# Now put it where we were told to.
r = requests.put(upload_url, data=image_data)
if r.status_code != 200:
print(
f"Error uploading image to external storage: got {r.status_code}", file=sys.stderr)
sys.exit(1)
# And tell Discourse that it worked, and get back an id we can reference later.
r = requests.post(f"https://{DISCOURSE}/uploads/complete-external-upload",
data=f'unique_identifier={upload_uid}', headers=HEADERS)
if r.status_code != 200:
print(f"Error completing upload: got {r.status_code}", file=sys.stderr)
sys.exit(1)
image_id = r.json()['id']
This works with an “all access” API key, but when I try to use a granular one with the “uploads: create” scope, I get a 403 error on post to /uploads/generate-presigned-put.
Hi @mattdm I’ve updated the API key scope to cover these new actions for our direct upload flow, and I’ve deployed your site too, please let me know if you still have further issues.