我正在尝试编写一些基本代码来通过 Node.js 创建帖子。这是针对由 Wordpress.com 托管的 WP 安装。我为用户启用了 2FA 并设置了应用程序密码。然而,我似乎无法过去:
{
code: 'rest_cannot_create',
message: 'Sorry, you are not allowed to create posts as this user.',
data: { status: 401 }
}
据我所知,创建应用程序密码应该可以使简单的身份验证方法发挥作用,但也许我遗漏了一些东西?这是删除用户名/密码/主机的代码:
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);
您在授权字符串中连接了两次“Basic”一词。
到达这里后:
let auth = 'Basic ' + Buffer.from(username + ':' + password).toString('base64');
再次在这里:
'Authorization':'Basic '+auth,