woocommerce_rest_cannot_create在jwt身份验证上

时间:2018-06-21 10:21:49

标签: wordpress rest woocommerce wordpress-rest-api woocommerce-rest-api

我正在将WordPress JWT身份验证与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
    }
}

1 个答案:

答案 0 :(得分:0)

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