在 Postman 上使用 Wordpress Nonce 作为 setRequestHeader,如 jQuery/Javascript
Use Wordpress Nonce on Postman as a setRequestHeader like in jQuery/Javascript
这是我的问题。
实际上,我似乎是在尝试解决错误而不是解决问题,这就是原因:
我正在创建一个在 WordPress 环境中运行良好的插件,并且我使用 WordPress 随机数作为身份验证模式。
我正在使用 jQuery/Ajax 传递我之前使用 PHP:
创建的 header 中的随机数
PHP:
wp_register_script('front-main', plugins_url('js/front-main.js' , __FILE__ ), '', '', true );
wp_enqueue_script('front-main');
wp_localize_script( 'front-main', 'wpApiSettings', array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
) );
Javascript/jQuery:
$.ajax({
method: 'GET',
url: wpApiSettings.root+'top-list-route/my-top-list-get',
contentType: 'application/json; charset=utf-8',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
dataType: 'json',
success: ajaxResponse
});
function ajaxResponse(data) {
console.log(data)
}
到目前为止一切顺利,简而言之,此应用程序将在我之前使用 PHP:
创建的这条路线上运行
public function my_register_route() {
register_rest_route( 'top-list-route', 'my-top-list-get', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'my_top_list_get'),
'permission_callback' => function() {
return current_user_can( 'edit_posts' );
},
),
) );
现在,如果我尝试 运行 使用 Postman 的随机数(我 console.log 在我的浏览器上)的相同代码, visual studio 代码(带有 REST 客户端的扩展) 或者只是在 chrome 上的 URL 中,它不会起作用,例如:
邮递员:
GET http://netzstrategen.local/wp-json/top-list-route/my-top-list-get
(IN THE HEADERS section) X-WP-Nonce (key) 47489127d8 (value, for example)
将return:
{
"code": "rest_cookie_invalid_nonce",
"message": "Cookie nonce is invalid",
"data": {
"status": 403
}
}
或者如果我使用这个 URL 例如:
http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
它将 return 相同的状态 (403 rest_cookie_invalid_nonce)。
与 visual studio 代码相同的问题,如果我在我的 GET 请求中添加任何 header,例如:
GET http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
将return:
HTTP/1.1 403 Forbidden
{
"code": "rest_cookie_invalid_nonce",
"message": "Cookie nonce is invalid",
"data": {
"status": 403
}
}
有什么提示吗?
这可能有点晚,但希望是其他人可能需要的解决方案,这就是我在 functions.php
中传递随机数的方式
wp_localize_script( 'app', 'WP_API_Settings', array(
'endpoint' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
) );
在 Postman 中,我通过请求传递随机数 header
X-WP-Nonce:{{nonce}}
Cookie:{{cookie}}
我注意到随机数已过期,您必须将新值传递给 Postman。我正在使用一个变量,以便它适用于我在 collection 中的所有路线。如果你得到一个无效的 nonce,请确保你是通过 Web 登录的,使用 Chrome 开发人员工具,F12 并在控制台中输入 wpApiSettings.nonce,这显示最新的 nonce 值。将该值复制并粘贴到您的变量中,或使用原始值并应用于 header.
我希望这对您有所帮助,弄清楚这个问题很痛苦!
这是我的问题。
实际上,我似乎是在尝试解决错误而不是解决问题,这就是原因:
我正在创建一个在 WordPress 环境中运行良好的插件,并且我使用 WordPress 随机数作为身份验证模式。
我正在使用 jQuery/Ajax 传递我之前使用 PHP:
创建的 header 中的随机数PHP:
wp_register_script('front-main', plugins_url('js/front-main.js' , __FILE__ ), '', '', true );
wp_enqueue_script('front-main');
wp_localize_script( 'front-main', 'wpApiSettings', array(
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
) );
Javascript/jQuery:
$.ajax({
method: 'GET',
url: wpApiSettings.root+'top-list-route/my-top-list-get',
contentType: 'application/json; charset=utf-8',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
dataType: 'json',
success: ajaxResponse
});
function ajaxResponse(data) {
console.log(data)
}
到目前为止一切顺利,简而言之,此应用程序将在我之前使用 PHP:
创建的这条路线上运行public function my_register_route() {
register_rest_route( 'top-list-route', 'my-top-list-get', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this, 'my_top_list_get'),
'permission_callback' => function() {
return current_user_can( 'edit_posts' );
},
),
) );
现在,如果我尝试 运行 使用 Postman 的随机数(我 console.log 在我的浏览器上)的相同代码, visual studio 代码(带有 REST 客户端的扩展) 或者只是在 chrome 上的 URL 中,它不会起作用,例如:
邮递员:
GET http://netzstrategen.local/wp-json/top-list-route/my-top-list-get
(IN THE HEADERS section) X-WP-Nonce (key) 47489127d8 (value, for example)
将return:
{
"code": "rest_cookie_invalid_nonce",
"message": "Cookie nonce is invalid",
"data": {
"status": 403
}
}
或者如果我使用这个 URL 例如:
http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
它将 return 相同的状态 (403 rest_cookie_invalid_nonce)。
与 visual studio 代码相同的问题,如果我在我的 GET 请求中添加任何 header,例如:
GET http://netzstrategen.local/wp-json/top-list-route/my-top-list-get?_wpnonce=47489127d8
将return:
HTTP/1.1 403 Forbidden
{
"code": "rest_cookie_invalid_nonce",
"message": "Cookie nonce is invalid",
"data": {
"status": 403
}
}
有什么提示吗?
这可能有点晚,但希望是其他人可能需要的解决方案,这就是我在 functions.php
中传递随机数的方式wp_localize_script( 'app', 'WP_API_Settings', array(
'endpoint' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
) );
在 Postman 中,我通过请求传递随机数 header
X-WP-Nonce:{{nonce}}
Cookie:{{cookie}}
我注意到随机数已过期,您必须将新值传递给 Postman。我正在使用一个变量,以便它适用于我在 collection 中的所有路线。如果你得到一个无效的 nonce,请确保你是通过 Web 登录的,使用 Chrome 开发人员工具,F12 并在控制台中输入 wpApiSettings.nonce,这显示最新的 nonce 值。将该值复制并粘贴到您的变量中,或使用原始值并应用于 header.
我希望这对您有所帮助,弄清楚这个问题很痛苦!