无法使用WP REST API 2.0插件通过基本身份验证进行身份验证

时间:2016-04-07 08:43:37

标签: php wordpress rest authentication wordpress-rest-api

我遇到基本身份验证问题。

尝试使用以下网址发送带Postman(chrome插件)的GET请求: 的 http://_MY_WEBSITE_URL_/wp-json/wp/v2/users/3

用户名和密码字段填写了网站的管理员用户凭据。

我得到的错误:

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

我尝试使用来自其他网站的wp_remote_request进行基本身份验证,并使用CURL,但结果每次都相同。

身份3的用户存在,我已经检查过了。如果我想列出所有用户,我只会获得创建帖子的人。

我已激活所需的插件: WP REST API JSON基本身份验证

我的wordpress版本: 4.4.2

3 个答案:

答案 0 :(得分:2)

我认为问题不是从服务器获取用户数据,但此错误代码是针对您的身份验证问题 具有此用户功能或角色可能不是管理员

详情视图

可湿性粉剂内容/插件/ REST的API / LIB /端点/类-WP-其余用户-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;
}

答案 1 :(得分:2)

最后,我找到了解决方案。我必须手动将一些新选项添加​​到我的 .htaccess 文件中,该插件无法实现。

代码:

<EditText
            android:id="@+id/imageDescriptionTextEdit"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:hint="Enter a description of this picture.."
            android:textSize="15sp"
            android:textColorHint="#E9E9E9"
            android:textColor="#ffffff"
            android:maxEms="26"
            android:imeOptions="actionDone"
            android:typeface="monospace"
            android:minEms="3"
            android:singleLine="true"
            fontPath="fonts/VarelaRound-Regular.ttf"
            tools:ignore="MissingPrefix"/>

答案 2 :(得分:0)

对于所有遇到这些错误的用户,在激活JWT后都必须删除标头中的基本身份验证授权,并在所有情况下(get和post)将customer_key和customer_secret发送为查询参数。这看似奇怪又不安全,但对我有用。

相关问题