在 WordPress 中 REST-API 只允许 "get"

Only "get" is allowed by REST-API in WordPress

在我的 WordPress 方面,我安装了 REST-API 插件并成功地通过以下方式获取帖子:

mywp.org/wp-json/wp/v2/posts

但是,只允许"get",如果我做任何其他事情,它return 401 "post"。我正在使用 chrome app "Postman" 和 Basic Auth.

我得到的:

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

这里是 header:

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
Connection →Keep-Alive
Content-Length →111
Content-Type →application/json; charset=UTF-8

我发现了一个问同样问题的问题 here 但答案对我不起作用。

我是 rest-api 的新人,不确定我应该做什么。

谢谢!

我自己找到了答案。由于我太粗心而错过了文档中的 "Install Basic Auth plugin in WP" 部分。 安装并启用后,我现在可以 "post" 和 "delete" 发帖了。

如果您使用的是从 Bitnami 部署的 WordPress 实例,并且遇到如下错误:

  • "woocommerce_rest_cannot_create" ("Sorry, you are not allowed to create resources.")
  • "rest_cannot_create" ("Sorry, you are not allowed to create posts as this user.")
  • "rest_cannot_create_user" ("Sorry, you are not allowed to create new users")

以下是为我解决问题的方法:

  • 安装 Basic Auth,您可以通过将 repo 克隆到 /opt/bitnami/apps/wordpress/htdocs/wp-content/plugins 来安装它,或者您也可以将 repo 作为 zip 下载并从 WP Admin 的插件页面中的文件安装。
  • 编辑 /opt/bitnami/apps/wordpress/htdocs
  • 中可用的 .htaccess 文件
  • RewriteEngine On下添加新行并插入RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]保存退出
  • 编辑 /opt/bitnami/apps/wordpress/conf/httpd-app.confRewriteEngine On
  • 之后添加新行
  • 在新行中插入:RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]保存并退出
  • 重新启动 Apache:sudo /opt/bitnami/ctlscript.sh restart apache
  • (可选)安装 Application Passwords By George Stephanis

更多详情请查看: https://community.bitnami.com/t/setting-up-api-access-to-wordpress-on-aws-ec2-instance/60589/7

我在处理 WP API 时遇到了同样的问题,我通过在 .htaccess 文件中添加三行解决了这个问题。

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=

您可以关注link了解更多详情。

More details to add lines in .htaccess