Can't authenticate with basic authentication using WP REST API 2.0 plugin

拈花ヽ惹草 提交于 2019-12-01 08:05:21

Finally, I figured out the solution. I had to add some new options manually to my .htaccess file, the plugin didn't make it.

The code:

# 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
Pranav Bhatt

I think issue is not for getting user data from server but this error code is for your authentication problem have this user capability or Role might not be administrator

for detail view

wp-content/plugins/rest-api/lib/endpoints/class-wp-rest-users-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;
}

To all those who are facing these error,remove the Basic Authentication authorization in headers and send the customer_key and customer_secret as query parameter in all case( get and post) after activating JWT. This might seem odd and insecure but it works for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!