经过几次研究,我无法解决这个问题,同时根据WordPress API手册对当前进行身份验证,我仍然有403个错误。
我不想使用像JWT认证插件这样的第三方插件
如果我做错了什么,请告诉我。
我正在使用"@supercharge/sttp":"^1.3.0"包装器库Axios执行以下任务
package.json
"@supercharge/sttp": "^1.3.0",
single.php
<div data-nonce="<?php wp_create_nonce('wp_rest') ?>" id="root"></div>
index.js
const el = document.getElementById("root");
export const nonce = el.getAttribute('data-nonce');
api.js
import { nonce } from '../index';
export const API_URL = 'https://localhost:3000/wp-json/wpplugin/v1';
export const setCurrentBid = async ( id, collection ) => {
return await Sttp
.withHeaders({ 'X-WP-Nonce': dataNonce })
.withPayload( collection )
.post(API_URL + '/items/' + id + '/data');
};
根据WordPress API手册,如果我发送‘X Nonce’,那么我不需要在WordPress API端点中进行验证。
所有的麻烦都是在我的端点中使用wp_get_current_user()
public function update_item_collection_data_callback( WP_REST_Request $request ) {
$params = $request->get_params();
$j = $request->get_json_params();
$record = wp_get_current_user();
return new WP_REST_Response($record, 200);
}
响应
{
"code":"rest_cookie_invalid_nonce",
"message":"Cookie check failed",
"data":{"status":403}
}
发布于 2022-08-04 09:12:23
这是愚蠢的,一切都是正确的,除了,我应该已经登录,然后登录后,实现了现在的身份验证。
https://stackoverflow.com/questions/73221937
复制相似问题