简体   繁体   English

X-WP-Nonce == Cookie nonce 无效

[英]X-WP-Nonce == Cookie nonce is invalid

On my WordPress REST endpoint, I did a login on wordpress to return a nonce like so:在我的 WordPress REST 端点上,我在 wordpress 上登录以返回如下随机数:

PHP PHP

$authenticated   = wp_authenticate( $userID , $password );
    $isAuthenticated = ( $authenticated instanceof \WP_User ) ? TRUE : FALSE;

    if ( $isAuthenticated )
    {
        $responseData[ "nonce" ] = wp_create_nonce( "rest" );

        return rest_ensure_response( $responseData );
     }

Then I returned the nonce through axios back to PHP to verify it and it works!然后我通过 axios 将 nonce 返回给 PHP 以验证它并且它有效!

JS: JS:

let axiosSettings = {
        baseURL: "http://site.localhost",
        url: "/wp-json/id3/test/verify",
        method: "POST",
        data: {
            n: this.state.nonce
        }
}

But when I put the nonce in the header X-WP-Nonce,但是当我将 nonce 放在标题 X-WP-Nonce 中时,

let axiosSettings = {
        baseURL: "http://site.localhost",
        url: "/wp-json/id3/test/pc",
        method: "POST",
        data: {
            n: this.state.nonce
        },
        withCredentials: true,
        headers: {
            "X-WP-Nonce": this.state.nonce
        }
    };

it tells me the它告诉我

Cookie nonce is invalid and refuses to access my REST API. Cookie nonce 无效并且拒绝访问我的 REST API。 Why's that?那是为什么?

Lots of searching later ... the action needs to be "wp_rest".稍后进行大量搜索......动作需要是“wp_rest”。

How to get current logged in user using Wordpress Rest Api? 如何使用 Wordpress Rest Api 获取当前登录用户?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM