401未经授权使用Wordpress Basic Auth插件

时间:2018-02-13 14:47:43

标签: wordpress wordpress-rest-api wordpress-json-api wordpress-admin

我已经安装了Wordpress团队制作的插件: https://github.com/WP-API/Basic-Auth

我正在使用Wordpress 4.9.4发出此请求:

GET http://somehostname.com/index.php?rest_route=%2Fwp%2Fv2%2Fposts&per_page=100&page=2&context=edit HTTP/1.1
Host: somehostname.com
Authorization: Basic [****base64encoded username+":"+pass *******]
Accept-Encoding: gzip, deflate
User-Agent: [some user agent name]

响应:

HTTP/1.1 401 Unauthorized
Date: Tue, 13 Feb 2018 14:26:12 GMT
Server: Apache
X-Powered-By: PHP/7.1.12
X-Robots-Tag: noindex
Link: <http://somehostname.com/wp-json/>; rel="https://api.w.org/"
X-Content-Type-Options: nosniff
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
Access-Control-Allow-Headers: Authorization, Content-Type
Vary: Accept-Encoding,User-Agent
Content-Length: 127
Content-Type: application/json; charset=UTF-8

{"code":"rest_forbidden_context","message":"Sorry, you are not allowed to edit posts in this post type.","data":{"status":401}}

4 个答案:

答案 0 :(得分:1)

请在您的htaccess文件上添加以下代码。

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

,如果您现在以wordpress管理员身份登录,则不允许您创建帖子,因此请先从wordpress admin中注销,然后再尝试进行请求。

这两个解决方案解决了我的问题。

答案 1 :(得分:0)

我从你的问题推断,这不应该发生。您期望某些响应代码不属于4xx种类。

从标题:“401未经授权使用Wordpress Basic Auth插件”,我也在实现飞跃,但您可能认为该插件不允许您在任何<中访问该路径/ em>感觉。

因此,考虑到这一点,我想指出错误数据告诉您一些非常精确的事情: user 不允许编辑该类型的帖子。 用户的凭据无效相同。

{
    "code": "rest_forbidden_context",
    "message": "Sorry, you are not allowed to edit posts in this post type.",
    "data": {
        "status": 401
    }
}

尝试确认您以[****base64encoded username+":"+pass *******]格式提供的凭据的用户是管理员/作者/编辑(或其他角色),该用户有权编辑您尝试访问的类型的帖子。

修改:请参阅WP-API的this line,该错误可能来自哪里

答案 2 :(得分:0)

我刚刚修复此问题,因为此评论建议: Fix for basic OAuth 不确定此修复程序是否有任何副作用?

答案 3 :(得分:0)

我按照以下简短建议解决了身份验证问题

https://github.com/WordPress/application-passwords/wiki/Basic-Authorization-Header----Missing

我还必须将wordpress块移至htaccess的顶部