wordpress - woocommerce_rest_cannot_create 关于 jwt 身份验证
问题描述
我正在使用 WordPress JWT auth 以及 wp-rest API 和 woocommerce。当我尝试通过 API从订阅者帐户下订单时,它显示“ woocommerce_rest_cannot_create ”。使用 woocommerce rest API 使用 JWT auth 创建订单的正确程序是什么。
{
"code": "woocommerce_rest_cannot_create",
"message": "Sorry, you are not allowed to create resources.",
"data": {
"status": 403
}
}
解决方案
您可以通过运行以下代码来检查您是否已正确设置 JWT:
# From the file includes/class-jwt-auth.php of the JWT Authentication for WP REST API plugin
# find the priority of its filter 'determine_current_user' and set $priority to that priority + 1.
# The GitHub version has 10 so this sample uses 11.
$priority = 11;
add_filter( 'determine_current_user', function( $current_user_id ) {
# If JWT Authentication has succeeded $current_user_id should be greater than 0
error_log( 'current user id = '. $current_user_id );
return $current_user_id;
}, $priority, 1 );
您可以通过运行以下代码来检查是否正在接收访问令牌:
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
error_log("$header: $value";
}
应该有如下标题:
Authorization: Bearer mF_s9.B5f-4.1JqM
推荐阅读
- python - 大型 ndarray 上的 Tensorflow 推断:cython 或 muliprocessing 加速可能吗?
- docker - cURL 请求挂在 docker ubuntu/alpine 容器中
- reactjs - 需要帮助架构 Redux 应用程序(减少所需操作的数量)
- ios - 在重新加载 SKScene 之前尝试使用 removeAllChildren() 和 removeAllActions() 时产生致命错误
- draftjs - 如何更改 Draftjs 中 ContentBlock 渲染的顺序?
- mongodb - 在 Mongo 聚合中使用 Cursor() 和 Mongoose
- javascript - 如果条件在不应该满足的情况下得到满足
- c++ - 有没有办法让 C++ 函数采用具有相同成员的两种不同类型?
- docker - AWS ECS DNS 解析失败
- javascript - Graph.js 页面不工作