如何在Java中正确调用Business Profile Performance API?请求报错400
解决Java调用Business Profile Performance API的400错误
你的代码出现400 Bad Request是因为调用getDailyMetricsTimeSeries接口时,缺少了两个必填参数:需要获取的指标类型,以及查询的时间范围(起始日期和结束日期)。官方API要求这两个参数必须传入,否则会返回无效参数错误。
修正后的代码示例
try { // 指定要获取的指标,比如"ALL_VIEWS"、"WEBSITE_CLICKS"等,支持多选 List<String> metrics = Arrays.asList("ALL_VIEWS", "WEBSITE_CLICKS"); // 设置时间范围,格式为YYYY-MM-DD Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse("2024-01-01"); Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse("2024-01-31"); // 构建请求并添加必填参数 BusinessProfilePerformance.Locations.GetDailyMetricsTimeSeries metricReport = businessProfilePerformance.locations() .getDailyMetricsTimeSeries(locationName) .setMetrics(metrics) .setStartDate(startDate) .setEndDate(endDate); GetDailyMetricsTimeSeriesResponse response = metricReport.execute(); if (!response.isEmpty()) { System.out.println("Response== " + response); } } catch (Exception e) { e.printStackTrace(); // 建议用printStackTrace替代println,能查看完整错误栈信息 }
额外注意事项
- locationName格式检查:确保
locationName的格式为locations/{你的店铺ID},比如locations/1234567890,格式错误也会触发无效参数报错。 - 指标合法性:使用官方支持的指标名称,比如
ALL_VIEWS、SEARCH_VIEWS、MAP_VIEWS、DIRECTIONS_REQUESTS等,不要使用自定义名称。 - 权限验证:确认授权账号拥有该店铺的Performance数据访问权限,权限不足通常会返回403错误,但也可能伴随参数类报错混淆判断。
内容的提问来源于stack exchange,提问作者Shila Mosammami
相关产品推荐
相关产品推荐

