首页 > 解决方案 > 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
    }
}

标签: wordpressrestwoocommercewordpress-rest-apiwoocommerce-rest-api

解决方案


您可以通过运行以下代码来检查您是否已正确设置 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

推荐阅读