只能访问方法“GET"在 WP REST API 中 - 其他人返回 401 [英] Only have access to method "GET" in WP REST API - others return 401

查看:34
本文介绍了只能访问方法“GET"在 WP REST API 中 - 其他人返回 401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working with the Wordpress REST API and have the following issue:

当我尝试发出POST"或DELETE"请求时,我收到 401 错误 - 即使对于wp-json/wp/v2/posts"等标准端点也是如此.

When I try to make a "POST" or "DELETE" request I get a 401 error - even for the standard endpoints like "wp-json/wp/v2/posts".

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

Locally it works without any problems, but on my webserver it doesn't.

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

Here's the response from when I try to make a "POST" request to the server:

{
  "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"-请求wp-json/wp/v2/posts"的标头:

Here's the are the Headers for a "POST"-Request for "wp-json/wp/v2/posts" from my local server:

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".

As you can see, locally both "GET" and "POST" are allowed, on the server only "GET".

您知道问题出在哪里吗?

Do you have any idea, where the problem could be?

推荐答案

经过数小时的搜索,我终于解决了这个问题!

I was able to fix this issue after hours of search!

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

The problem was that .htaccess was missing these mod_rewrite rules:

# 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

干杯

这篇关于只能访问方法“GET"在 WP REST API 中 - 其他人返回 401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆