You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java调用Business Profile Performance API获搜索关键词遇400错误求助

解决Business Profile Performance API 400无效参数错误

问题描述

我正在使用Business Profile Performance API获取用户在搜索或地图中找到商家所用的搜索关键词。已初始化完成凭证配置的BusinessProfilePerformance类型变量business_profile_performance,编写代码如下:

try { 
    BusinessProfilePerformance.Locations.Searchkeywords.Impressions.Monthly.List impressionsList = business_profile_performance
        .locations().searchkeywords().impressions().monthly().list("locations/" + locationName);               
    ListSearchKeywordImpressionsMonthlyResponse response = impressionsList.execute();
    List<SearchKeywordCount> impressions = response.getSearchKeywordsCounts();
    System.out.println("Size is " + impressions.size());
} catch (IOException e) {
    e.printStackTrace();
}

执行后返回400错误:

{
  "code" : 400,
  "details" : [ {
    "@type" : "type.googleapis.com/google.rpc.BadRequest"
  } ],
  "errors" : [ {
    "domain" : "global",
    "message" : "Request contains an invalid argument.",
    "reason" : "badRequest"
  } ],
  "message" : "Request contains an invalid argument.",
  "status" : "INVALID_ARGUMENT"
}

解决方法

  • 修正Location参数格式
    API要求的位置参数格式为locations/{locationId},需确认locationName是否已经包含locations/前缀。如果locationName本身是纯ID(如123456789),拼接后会变成locations/locations/123456789,这会导致无效参数。正确写法应为直接传入"locations/" + locationId,或者直接使用完整的位置资源名(如果locationName已经包含前缀)。

  • 验证API权限与启用状态
    确保Google Cloud项目已启用Business Profile Performance API,且凭证对应的账号拥有该商家位置的访问权限(如商家所有者或授权管理员)。权限不足有时会被包装为无效参数错误返回。

  • 补充必要的请求参数
    部分场景下需指定查询的时间范围,可添加年月参数(格式为YYYY-MM):

    impressionsList.setMonthYear("2024-05"); // 替换为你需要查询的年月
    
  • 调试实际请求URL
    添加日志打印实际请求URL,确认参数格式是否正确:

    System.out.println("实际请求URL: " + impressionsList.buildHttpRequestUrl());
    

    对比API文档的示例URL,检查格式是否一致。

  • 检查字符编码问题
    如果locationName包含特殊字符,确保已正确URL编码。Google Java客户端通常会自动处理编码,但手动拼接时需注意避免格式错误。

内容的提问来源于stack exchange,提问作者Shila Mosammami

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 10:50:15