【问题标题】:How to fix 401 Unauthorized response WP Rest API如何修复 401 Unauthorized response WP Rest API
【发布时间】:2020-07-05 23:31:05
【问题描述】:

我正在向我们网络上的另一个 wordpress 站点发出请求,如下所示。

    //Send the request to update the submission post
    $response = wp_remote_request( $this->urls->assign_url, array(
            'headers'     => array(
                'Content-Type' => 'application/json; charset=utf-8',
                'Authentication' => 'Basic '.base64_encode('somename:somepassword')
            ),
            'body'        => json_encode($array),
            'method'      => 'POST',
            'data_format' => 'body'
        )
    );
  • 我正在通过 ajax 发出此请求。
  • 正在调用回调函数并发送回数据。
  • 我也登录了远程站点。
  • 我使用的是随机数,并且在标题中获得授权的用户是有效用户。

我不断回复的是:

body: "{"code":"rest_not_logged_in","message":"You are not currently logged in.","data":{"status":401}}"

自从我更新了远程 wordpress 版本后,我才刚刚开始得到这个。在那之前它工作得很好。

任何想法。

【问题讨论】:

  • 您使用的是默认认证还是插件?我在您的请求中没有看到 nonce,但可能是误解。
  • Nonce 在 js 中传递给回调函数,并在这个 http 请求之前检查。我没有使用插件,这只是默认用户身份验证。该用户是远程站点上的简单管理员用户。

标签: wordpress wordpress-rest-api


【解决方案1】:

我相信为了以您想要的方式进行身份验证,您需要使用插件 - 内置的身份验证方法不适合异地请求,因为它是基于 cookie 的。

https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#authentication-plugins

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2019-12-05
    • 2014-08-19
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多