wordpress - 将 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 的标题上得到了响应
知道如何解决这个问题,以便我得到所有的骄傲吗?
解决方案
好的,感谢这篇文章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 请求时,它就可以正常工作了。
推荐阅读
- php - 将日期动态分配给数组中的多个值 - php
- haskell - 我们能想到 Haskell 中的非对称乘积数据类型吗?
- mysql - SQL 实现 - 类似社交媒体的功能
- facebook - Facebook 开发者应用审查 - 您的提交必须包含 pages_read_engagement 才能使用 instagram_basic
- python - 如何使用 unittest 显示测试用例中的错误
- reactjs - 刷新页面后不过滤反应下拉选择的值
- python - 在使用 vgg 模型计算模型后的预测期间“进程停止”
- reactjs - 在 CSS 中使用 Material-UI 主题颜色
- http - SIM5360A - HTTP 错误请求
- r - R包异常化在分组的非时间序列数据上失败