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

使用Google Business Profile API创建地点时遇无效参数错误

问题描述

使用Node.js的googleapis包调用Google Business Profile API添加地点时,返回错误:

{"error": "Request contains an invalid argument."}

即使仅保留必填字段仍报错,代码如下:

app.post('/add-location', async (req, res) => {
    const { location, tokens } = req.body;
    oauth2Client.setCredentials(tokens);

    const mybusinessbusinessinformation = google.mybusinessbusinessinformation('v1');
    try {
        const response = await mybusinessbusinessinformation.accounts.locations.create({
            parent: 'accounts/' + process.env.ACCOUNT_ID,
            validateOnly: true,
            requestId: uuidv4(),
            requestBody: {
                "title": "EVgo Charging Station",
                "profile": {
                    "description": "Fast and reliable electric vehicle charging station."
                },
            },
            auth: oauth2Client
        });
        console.log(response)
        res.send(response.data);
    } catch (error) {
        console.error(error)
        res.status(400).send({ error: error.message });
    }
});

详细错误日志:

GaxiosError: Request contains an invalid argument.
    at Gaxios._request (/Users/workstation/Programming/Clients/-/gmb-microservice/node_modules/gaxios/build/src/gaxios.js:142:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async OAuth2Client.requestAsync (/Users/workstation/Programming/Clients/-/gmb-microservice/node_modules/google-auth-library/build/src/auth/oauth2client.js:429:18)
    at async /Users/workstation/Programming/Clients/-/gmb-microservice/server.js:52:26 {
  config: {
    url: 'https://mybusinessbusinessinformation.googleapis.com/v1/accounts/102094468378068712054/locations?validateOnly=true&requestId=54e30089-a836-441c-8eb0-bdd936c0a979',
    method: 'POST',
    apiVersion: '',
    userAgentDirectives: [ [Object] ],
    paramsSerializer: [Function (anonymous)],
    data: { title: 'EVgo Charging Station', profile: [Object] },
    headers: {
      'x-goog-api-client': 'gdcl/7.2.0 gl-node/20.8.0',
      'Accept-Encoding': 'gzip',
      'User-Agent': 'google-api-nodejs-client/7.2.0 (gzip)',
      Authorization: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
      'Content-Type': 'application/json'
    },
    params: {
      validateOnly: true,
      requestId: '54e30089-a836-441c-8eb0-bdd936c0a979'
    },
    validateStatus: [Function (anonymous)],
    retry: true,
    body: '{"title":"EVgo Charging Station","profile":{"description":"Fast and reliable electric vehicle charging station."}}',
    responseType: 'unknown',
    errorRedactor: [Function: defaultErrorRedactor],
    retryConfig: {
      currentRetryAttempt: 0,
      retry: 3,
      httpMethodsToRetry: [Array],
      noResponseRetries: 2,
      retryDelayMultiplier: 2,
      timeOfFirstRequest: 1722445004419,
      totalTimeout: 9007199254740991,
      maxRetryDelay: 9007199254740991,
      statusCodesToRetry: [Array]
    }
  },
  response: {
    config: {
      url: 'https://mybusinessbusinessinformation.googleapis.com/v1/accounts/102094468378068712054/locations?validateOnly=true&requestId=54e30089-a836-441c-8eb0-bdd936c0a979',
      method: 'POST',
      apiVersion: '',
      userAgentDirectives: [Array],
      paramsSerializer: [Function (anonymous)],
      data: [Object],
      headers: [Object],
      params: [Object],
      validateStatus: [Function (anonymous)],
      retry: true,
      body: '{"title":"EVgo Charging Station","profile":{"description":"Fast and reliable electric vehicle charging station."}}',
      responseType: 'unknown',
      errorRedactor: [Function: defaultErrorRedactor]
    },
    data: { error: [Object] },
    headers: {
      'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000',
      'cache-control': 'private',
      'content-encoding': 'gzip',
      'content-type': 'application/json; charset=UTF-8',
      date: 'Wed, 31 Jul 2024 16:56:44 GMT',
      server: 'ESF',
      'transfer-encoding': 'chunked',
      vary: 'Origin, X-Origin, Referer',
      'x-content-type-options': 'nosniff',
      'x-frame-options': 'SAMEORIGIN',
      'x-xss-protection': '0'
    },
    status: 400,
    statusText: 'Bad Request',
    request: {
      responseURL: 'https://mybusinessbusinessinformation.googleapis.com/v1/accounts/102094468378068712054/locations?validateOnly=true&requestId=54e30089-a836-441c-8eb0-bdd936c0a979'
    }
  },
  error: undefined,
  status: 400,
  code: 400,
  errors: [
    {
      message: 'Request contains an invalid argument.',
      domain: 'global',
      reason: 'badRequest'
    }
  ],
  [Symbol(gaxios-gaxios-error)]: '6.7.0'
}

同时询问:googleapis npm包中是否有Google Business Profile API的详细中文文档?目前参考的是英文官方文档。


解决方案

1. 补充请求体必填字段

Google Business Profile API创建地点时,仅title和profile.description不满足必填要求,必须包含以下核心信息:

  • languageCode:地点使用的语言代码(如en-US)
  • address:完整地址结构(含街道、城市、邮编、国家代码)
  • primaryCategory:官方定义的有效分类ID

修改后的请求体示例:

requestBody: {
  "title": "EVgo Charging Station",
  "languageCode": "en-US",
  "address": {
    "regionCode": "US",
    "locality": "Los Angeles",
    "addressLines": ["123 Main St"],
    "postalCode": "90001"
  },
  "primaryCategory": {
    "categoryId": "gcid:electric_vehicle_charging_station"
  },
  "profile": {
    "description": "Fast and reliable electric vehicle charging station."
  }
}

2. 验证分类ID有效性

主分类ID需为Google官方提供的有效ID,可通过accounts.locations.categories.list接口获取对应行业的分类列表。

3. 文档相关说明

googleapis npm包本身没有单独的中文文档,可将官方英文文档翻译成中文使用,或参考国内技术社区的中文教程。官方文档的字段定义和接口参数为权威参考,需确保请求体严格遵循其要求。


内容的提问来源于stack exchange,提问作者Ahsan Aasim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 09:12:07