Publishing a post with API and Python doesn't work

if I use nodejs like this, the article publishes correctly:

payload = {
    posts: [{
        title: "My test post",
        html: "<p>My post content. Work in progress...</p>",
        status: "published"
    }]
};
response = await axios.post(urlInsert, payload, { headers })

if I use python it doesn’t work, the post isn’t even written:

post_data = {
    "posts": [{
        "title": "My test post",
        "html": "<p>My post content. Work in progress...</p>",
        "status": "published"
    }]
}
response = requests.post(url, headers = headers, json = post_data)

if instead I use draft the post is written, but obviously it is not published with python:

post_data = {
    "posts": [{
        "title": "My test post",
        "html": "<p>My post content. Work in progress...</p>",
        "status": "draft"
    }]
}
response = requests.post(url, headers = headers, json = post_data)

Are you authenticating in the same way, with the same key or jwt? This behavior might be consistent with a permissions error if you’re using a contributor key…

The Admin Key is the same, and this is the code:

nodejs:

token = jwt.sign({}, Buffer.from(secret, 'hex'), {
            keyid: id,
            algorithm: 'HS256',
            expiresIn: '5m',
            audience: `/v3/admin/`
        });
headers = { Authorization: `Ghost ${token}` };

python:

payload = {
    'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=5),
    'iat': datetime.datetime.utcnow(),
    'aud': '/v3/admin/'
}

token = jwt.encode(payload, bytes.fromhex(secret), algorithm='HS256', headers={'kid': id})
headers = { 'Authorization': f"Ghost {token}" }

How weird. Do you have access to logging from the Ghost site that might help with the diagnosis?

1 Like

I understood the problem, it wasn’t something related to the language used, I had a trigger on post.published that created problems.

Thanks for your help

1 Like