Hi fellow ghost users
I have students writing articles for a school paper. They use another CMS to produce a print version. I’d like to automatically publish their articles to a ghost blog using the Admin API with python.
I was able to publish articles under my name (as ghost blog owner) using the API. But I would prefer students get credit.
Here is what I do. After authorizing with jwt, I open a session with a given student’s credentials.
url = 'https://latelier.uqam.media/ghost/api/v3/admin/session/'
headers = {'Authorization': 'Ghost {}'.format(jeton.decode()),"Origin": "https://latelier.uqam.media"}
estudiante = {
"email": "Chuck_Norris@uqam.ca",
"password": whatever
}
r = requests.post(url, json=estudiante, headers=headers)
# I record the session cookie to file with pickle
with open("biscuits.pkl", "wb") as oreo:
pickle.dump(r.cookies, oreo)
The Ghost blog returns a 201 as status code, meaning success. The response headers include the session cookie, ending with «Hb4»:
{'Server': 'nginx/1.14.0 (Ubuntu)', 'Date': 'Mon, 09 Mar 2020 04:12:54 GMT', 'Content-Type': 'text/plain; charset=utf-8', 'Content-Length': '7', 'Connection': 'keep-alive', 'X-Powered-By': 'Express', 'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0', 'Access-Control-Allow-Origin': 'https://latelier.uqam.media', 'Vary': 'Origin, Accept-Encoding', 'ETag': 'W/"7-rM9AyJuqT6iOan/xHh+AW+7K/T8"', 'Set-Cookie': 'ghost-admin-api-session=s%3AZbe-lLt4rgGJ-FZs1S0pFUeDhbcDF-s4.%2Fd8H0M4pwDTaJG94Xm5SJVap%2FQWpns2fETzcgxYfHb4; Path=/ghost; Expires=Mon, 07 Sep 2020 16:12:54 GMT; HttpOnly; Secure; SameSite=Lax', 'Strict-Transport-Security': 'max-age=63072000; includeSubDomains; preload', 'X-Frame-Options': 'SAMEORIGIN', 'X-Content-Type-Options': 'nosniff'}
Using pickle, I include the cookie in a POST request to publish the student’s post:
url = "https://latelier.uqam.media/ghost/api/v3/admin/posts/?source=html", "Origin":"https://journalisme.uqam.ca/"}
headers = {"Authorization": "Ghost {}".format(jeton.decode()), "Credentials":"include", "Origin":"https://latelier.uqam.media/"}
body = {"posts": [{
"title": "Est-ce que le COVID-19 menace votre porte-monnaie?",
"authors": [estudiante["email"]],
"html": '<p >La Banque centrale américaine a baissé son taux directeur [...] à moins d\'une faillite.</p>',
"status": "published"
}
]}
with open("biscuits.pkl", "rb") as goglu:
biscuits = pickle.load(goglu)
r2 = requests.post(url, json=body, headers=headers, cookies=biscuits)
The headers of this second request show the cookie, ending with «Hb4», was passed along:
{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Authorization': 'Ghost <ChuckNorrisToldMeNotToDiscloseOrElse>', 'Credentials': 'include', 'Origin': 'https://latelier.uqam.media/', 'Cookie': 'ghost-admin-api-session=s%3AZbe-lLt4rgGJ-FZs1S0pFUeDhbcDF-s4.%2Fd8H0M4pwDTaJG94Xm5SJVap%2FQWpns2fETzcgxYfHb4', 'Content-Length': '4212', 'Content-Type': 'application/json'}
Yet, I get a 400 error (Bad Request). What am I doing wrong?
Thanks
JH Roy
Montreal