Problem in topic. Im using python 3.8, my code below:
filename = f"{url['url'].split('/')[-1][:11]}.jpg"
open(filename, 'wb').write(requests.get(url['url']).content)
filename, content = os.path.basename(open(filename, 'rb').name), open(filename, 'rb')
content_type, _ = mimetypes.guess_type(filename)
file_arg = (filename, content, content_type)
photourl = requests.post("https://{admin_domain}/ghost/api/admin/images/upload/",
headers = {'Content-Type': 'application/json', "Authorization": "Ghost {token}", "Accept-Version": "5.59", "Content-Type" : 'multipart/form-data;'},
files = {'file': file_arg},
params = {'purpose': 'image', 'ref': filename}
)
RyanF
August 29, 2023, 3:18pm
2
Are you receiving any other error messages?
How are you generating the token? Are you able to use the API with other methods?
oh i found my error while generating token, but now have another error
{"errors":[{"message":"Please select an image.","context":null,"type":"ValidationError","details":null,"property":null,"help":null,"code":null,"id":"45186620-467c-11ee-ab06-a7253d0c2a45","ghostErrorCode":null}]}
RyanF
August 29, 2023, 3:44pm
4
The error indicates that the API isn’t receiving your image.
I’d debug a couple basic things to see where the error might be:
Is the file an image?
Would reusing the file object help?
Content type is specified twice
ensure file_arg
is formatted properly
RyanF
August 29, 2023, 6:43pm
6
I’m not a Python expert, but do you need a read
method on the file content?
I copied this part of code from library ghost_client
on Github. And most guides on Google says what open(filename, 'rb')
right solution
I deleted Content-Type header and it works. Thanks for your help, it helps me
And can you help with creating post? Error - {"errors":[{"message":"An unexpected error occurred, please try again.","context":"Unexpected token p in JSON at position 0","type":"InternalServerError","details":null,"property":null,"help":null,"code":"UNEXPECTED_ERROR","id":"224b8510-46df-11ee-ab06-a7253d0c2a45","ghostErrorCode":null}]}
And my code -
requests.post("https://blog.artembay.ru/ghost/api/admin/posts/?formats=mobiledoc%2Clexical", data={
"posts": [
{
"title": text,
"lexical": {"root":{"children":[{"type":"gallery","version":1,"images":images,"caption":""},{"children":[],"direction":'null',"format":"","indent":0,"type":"paragraph","version":1}],"direction":'null',"format":"","indent":0,"type":"root","version":1}},
"feature_image": urls[0],
"status": "published",
"authors": [ {"id": "64ecd3b716aaef000168127d"} ],
"tags": [{ "name": "Из ВКонтакте", "description": "Этот пост был переслан из Вконтакте" }]
}
]
},
headers = {"Content-Type": "application/json", "Authorization": f"Ghost {token}", "Accept-Version": "5.59"}
)