只能访问WP REST API中的方法“GET” - 其他人返回401

时间:2016-04-06 14:12:00

标签: php wordpress wordpress-rest-api

我正在使用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
  }
}

......和HEADERS:

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” - 请求“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”。

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

1 个答案:

答案 0 :(得分:1)

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

问题是.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

干杯