【发布时间】:2016-11-07 21:14:13
【问题描述】:
我正在使用当前的 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);
}
但是我收到了这个错误:
{"code":"rest_cannot_create","message":"Sorry, you are not allowed to create new posts.","data":{"status":401}}
我阅读了其他类似的 StackOverflow 问题,他们通过更改 .htacess 文件解决了这个问题,不幸的是,这并没有解决我的问题。
这是我的.htaccess 文件
# BEGIN WP BASIC Auth
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</IfModule>
# END WP BASIC Auth
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteBase /
RewriteRule ^index\.php$ - [E=X-HTTP_AUTHORIZATION:% {HTTP:Authorization},QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
</IfModule>
# END WordPress
有人可以帮忙吗?
【问题讨论】:
-
您是否尝试过安装 basic-auth 插件?
-
我做了,但没有进展 - 我认为插件无法帮助我,因为这是最新的 WP (5.2.2)
-
你认证了吗?
标签: wordpress .htaccess google-apps-script