0

我正在使用 Wordpress REST API 并遇到以下问题:

当我尝试发出“POST”或“DELETE”请求时,我收到 401 错误 - 即使对于像“wp-json/wp/v2/posts”这样的标准端点也是如此。

在本地它可以正常工作,但在我的网络服务器上却没有。

这是我尝试向服务器发出“POST”请求时的响应:

{
  "code": "rest_cannot_create",
  "message": "Sorry, you are not allowed to create new posts.",
  "data": {
      "status": 401
  }
}

...和标题:

Access-Control-Allow-Credentials →true
Access-Control-Allow-Headers →Authorization
Access-Control-Allow-Methods →POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin →
Access-Control-Expose-Headers →X-WP-Total, X-WP-TotalPages
Age →0
Allow →GET
Connection →keep-alive
Content-Length →111
Content-Type →application/json; charset=UTF-8
Date →Wed, 06 Apr 2016 13:59:45 GMT
Server →Apache
X-Content-Type-Options →nosniff

这是来自我的本地服务器的“POST”-Request 的“wp-json/wp/v2/posts”的标头:

Access-Control-Allow-Credentials →true
Access-Control-Allow-Headers →Authorization
Access-Control-Allow-Methods →POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin →
Access-Control-Expose-Headers →X-WP-Total, X-WP-TotalPages
Allow →GET, POST
Cache-Control →no-cache, must-revalidate, max-age=0
Connection →keep-alive
Content-Type →application/json; charset=UTF-8
Date →Wed, 06 Apr 2016 13:57:19 GMT
Expires →Wed, 11 Jan 1984 05:00:00 GMT
Last-Modified →
Location →http://seebook.dev/wp-json/wp/v2/posts/25
Pragma →no-cache
Server →nginx
Transfer-Encoding →chunked
X-Content-Type-Options →nosniff
X-Powered-By →PHP/5.5.9-1ubuntu4.14

如您所见,在本地允许“GET”和“POST”,在服务器上只允许“GET”。

你有什么想法,问题可能出在哪里?

4

1 回答 1

0

经过数小时的搜索,我能够解决此问题!

问题是 .htaccess 缺少这些 mod_rewrite 规则:

# BEGIN WP BASIC Auth
<IfModule mod_rewrite.c>
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
</IfModule>
# END WP BASIC Auth

在这里找到了片段:https ://stackoverflow.com/a/36499422/3754201

干杯

于 2016-04-12T20:52:55.750 回答