Unable to authenticate using basic authentication using WP REST API 2.0

I have a problem with basic authentication.

Trying to send a GET request using Postman (chrome plugin) using the following URL: http: // _ MY_WEBSITE_URL_ / wp-json / wp / v2 / users / 3

The username and password field is populated with the user credentials of the admin site.

The error I am getting is:

{
    "code": "rest_user_cannot_view",
    "message": "Sorry, you cannot view this resource.",
    "data": {
        "status": 401
    }
}

I tried basic authentication with wp_remote_request from another site and with CURL too, but the results are the same every time.

User with id 3 exists, I checked it. If I want to list all the users, I get only those who have messages.

I activated the necessary plugins: WP REST API , Basic JSON Authentication .

Wordpress: 4.4.2

+4
2
, . .htaccess, .

:

# BEGIN WP BASIC Auth
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /PluginTest/
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
</IfModule>
# END WP BASIC Auth
+2

, , ,

-//-/Library//----controller.php

public function get_item_permissions_check( $request ) {

    $id = (int) $request['id'];
    $user = get_userdata( $id );
    $types = get_post_types( array( 'public' => true ), 'names' );

    if ( empty( $id ) || empty( $user->ID ) ) {
        return new WP_Error( 'rest_user_invalid_id', __( 'Invalid resource id.' ), array( 'status' => 404 ) );
    }

    if ( get_current_user_id() === $id ) {
        return true;
    }

    if ( 'edit' === $request['context'] && ! current_user_can( 'list_users' ) ) {
        return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you cannot view this resource with edit context.' ), array( 'status' => rest_authorization_required_code() ) );
    } else if ( ! count_user_posts( $id, $types ) && ! current_user_can( 'edit_user', $id ) && ! current_user_can( 'list_users' ) ) {
        return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you cannot view this resource.' ), array( 'status' => rest_authorization_required_code() ) );
    }

    return true;
}
+2

All Articles