I’m trying to write some basic code that will create a post via Node.js. This is for a WP install hosted by WordPress.com. I enabled 2FA for the user and made an application password. However, I can’t seem to get past:
{
code: 'rest_cannot_create',
message: 'Sorry, you are not allowed to create posts as this user.',
data: { status: 401 }
}
As far as I know, creating the app password should enable simple auth methods to work, but maybe I’m missing something? Here’s the code with username/password/host removed:
let username = 'the user name';
let password = '16 digit password';
let auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
let body = {
title:'new post',
content:'content',
status:'draft'
};
let resp = await fetch('https://the site/wp-json/wp/v2/posts', {
method:'POST',
headers:{
'Authorization':'Basic '+auth,
'Content-Type':'application/json',
},
body:JSON.stringify(body)
});
let data = await resp.json();
console.log(data);