调用wp_remote_post请求WHISE API返回You are not allowed to call this method如何解决
问题排查与解决方法
排查步骤
- 校验Bearer Token有效性
- 确认你获取Token时已经申请了
estates/list接口对应的访问权限,WHISE的API权限按接口粒度分配,未授权的接口即使持有有效Token也会被拦截 - 检查Token是否过期,可解析JWT格式的Token确认有效期、权限域是否符合要求
- 确认Token拼接格式正确,
Bearer后面跟Token的位置没有多余空格、换行符
- 确认你获取Token时已经申请了
- 确认请求必填参数完整
WHISE的/v1/estates/list接口要求请求体中必须携带ClientId参数,值为你在WHISE后台申请的客户端ID,缺失该参数会触发权限类报错 - 检查IP白名单配置
WHISE API默认会限制调用IP,你需要将WordPress站点所在服务器的公网IP添加到WHISE后台的API访问白名单中 - 确认接口请求方法正确
官方文档要求该接口使用POST请求,你当前使用的wp_remote_post方法符合要求,无需调整
修正后代码示例
$url = 'https://api.whise.eu/v1/estates/list'; // 补充必填的ClientId参数 $body = array( 'ClientId' => '替换为你的WHISE客户端ID', 'Filter' => array( 'languageId' => 'nl-BE'), ); $headers = [ 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json' ]; $response = wp_remote_post( $url, array( 'method' => 'POST', 'timeout' => 45, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => $headers, 'body' => json_encode($body), 'cookies' => array() ) ); if ( is_wp_error( $response ) ) { $error_message = $response->get_error_message(); echo "Something went wrong: $error_message"; } else { // 优化:直接提取响应体内容,避免打印多余响应头信息 $response_data = json_decode(wp_remote_retrieve_body($response), true); echo 'Response:<pre>'; print_r($response_data); echo '</pre>'; }
内容的提问来源于stack exchange,提问作者aquaenerg
相关产品推荐
相关产品推荐

