首页 > 解决方案 > 将 Woocommerce REST API 与 WP-API 的 JWT 身份验证一起使用时,无法获得超过 12 个结果

问题描述

我正在开发一个 React Native 应用程序并将其连接到 WP Woocommerce 后端。

当我过去通过 OAuth 1.0 请求数据时,一切正常,但由于我使用插件“WP-API 的 JWT 身份验证”更改为 JWT 身份验证,所以当我请求列出所有产品时,我最多只能获得 12 个结果。

我正在做的请求是使用不记名令牌,网址是 /wp-json/wc/v3/products?per_page=100

但是,我在“X-WP-Total 12” max 的标题上得到了响应

知道如何解决这个问题,以便我得到所有的骄傲吗?

标签: wordpressreact-nativewoocommercewordpress-rest-api

解决方案


好的,感谢这篇文章https://wordpress.org/support/topic/fix-basic-authentication-jwt_auth_bad_auth_header-error/

我不得不打开文件:

wp-content/plugins/jwt-authentication-for-wp-rest-api/public/class-jwt-auth-public.php

在第 250 行,上面写着

    /*
     * The HTTP_AUTHORIZATION is present verify the format
     * if the format is wrong return the user.
     */
    list($token) = sscanf($auth, 'Bearer %s');
    if (!$token) {
        return new WP_Error(
            'jwt_auth_bad_auth_header',
            'Authorization header malformed.',
            array(
                'status' => 403,
            )
        );
    }

将这些行替换为

if (!$token) {
        // Get token using basic auth
        list($username, $password) = explode( ':', base64_decode( substr( $auth, 6 ) ) );
        $request = new WP_REST_Request( 'POST', '/wp-json/jwt-auth/v1/token' );
        $request->set_param( 'username', $username );
        $request->set_param( 'password', $password );
        $JWT = new Jwt_Auth_Public('jwt-auth', '1.1.0');
        $token = $JWT->generate_token( $request );
        if (is_array($token) && isset($token['token'])) $token = $token['token'];
        return;
    }

这样做之后,当我们将后缀 ?per_page=100 添加到 GET 请求时,它就可以正常工作了。


推荐阅读