Hello,
I’m trying to fetch the posts from a local Ghost install using a Cloudflare Worker.
Here is what I tried and where I’m stuck.
Ghost Admin API library doesn’t seem to work since it requires Node.js modules like fs
for example. I was able to add in webpack config this and seems to be ok:
module.exports = {
target: "webworker",
entry: "./index.js",
node: {
fs: "empty"
}
}
Anyway … I tried without it to make sure that it doesn’t block anything else.
Now, I tried without the library. First, I tried to generate the Authorization token like this:
// Admin API key goes here
const key = MY_ADMIN_API_KEY;
// Split the key into ID and SECRET
const [id, secret] = key.split(':');
// Create the token (including decoding secret)
const token = jwt.sign({}, Buffer.from(secret, 'hex'), {
keyid: id,
algorithm: 'HS256',
expiresIn: '5m',
audience: `/v3/admin/`
});
return token;
The token seems to be generated ok.
If I try to use it like this:
const url = 'http://localhost:2368/ghost/api/v3/admin/posts/';
const headers = { Authorization: `Ghost ${token}` };
const payload = { posts: [{ title: 'Hello World' }] };
await axios.get(url, payload, { headers })
.then(response => console.log(response))
.catch(error => console.error(error));
I simply get error
without anything else.
So I moved to Postman and pasted the generated token into Authorization → Type: API Key
Key: Ghost
Value: Generated token at previous step
Add to Header
The response that I get is this:
{
"errors": [
{
"message": "Authorization failed",
"context": "Unable to determine the authenticated user or integration. Check that cookies are being passed through if using session authentication.",
"type": "NoPermissionError",
"details": null,
"property": null,
"help": null,
"code": null,
"id": "ff6286a0-6ef6-11ec-8255-f9406afb7644"
}
]
}
Any ideas what might be the issue here?