【问题标题】:401 Unauthorized with Wordpress Basic Auth Plugin401 Unauthorized with Wordpress Basic Auth Plugin
【发布时间】:2018-07-23 23:30:33
【问题描述】:

我已经安装了 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}}

【问题讨论】:

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


    【解决方案1】:

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

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

    如果您现在以 wordpress 管理员身份登录,那么它将不允许您创建帖子,因此请先从 wordpress 管理员注销,然后再尝试提出请求。

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

    【讨论】:

      【解决方案2】:

      我从你的问题推断出这不应该发生。您期待一些不属于4xx 的响应代码。

      从标题:“401 Unauthorized with Wordpress Basic Auth Plugin”,我也做了一个飞跃,但您可能认为该插件不允许您访问 any 感觉。

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

      {
          "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,该错误可能源于此

      【讨论】:

      【解决方案3】:

      我刚刚按照这条评论的建议解决了这个问题: Fix for basic OAuth 不确定此修复是否有任何副作用?

      【讨论】:

        【解决方案4】:

        我按照这个非常简短的建议解决了身份验证问题

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

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

        【讨论】:

          猜你喜欢
          • 2020-09-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-21
          • 2020-08-15
          • 2017-04-24
          • 1970-01-01
          相关资源
          最近更新 更多