我正在使用当前的Apps脚本功能将新帖子添加到我的WordPress网站:
var um_url = 'http://test.mywebsite.com/wp/wp-json/wp/v2/posts';
var um_headers = {
"Accept" : "application/json",
"Authorization": "Basic "+Utilities.base64Encode("user:passowrd"),
"Content-type":"application/json"
};
var um_options = {
"method":"POST",
"headers": um_headers,
"dataType" : 'json',
"data": {
title: "Foo title",
content: "Foo content",
status: "publish"
}
};
var um_response = UrlFetchApp.fetch(um_url, um_options);
var json = um_response.getContentText();
Logger.log(json);
}
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
{"code":"rest_cannot_create","message":"Sorry, you are not allowed to create new posts.","data":{"status":401}}
Run Code Online (Sandbox Code Playgroud)
我阅读了其他类似的StackOverflow问题,他们通过更改.htacess文件解决了问题,不幸的是,这并没有解决我的问题。这是我的.htaccess档案
# BEGIN WP BASIC Auth
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule …Run Code Online (Sandbox Code Playgroud)