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

使用My Business Business Information API遇400/401错误,寻求排查方案

使用My Business Business Information API获取地点列表的错误排查

已完成以下配置步骤:

  • 填写并提交基础配置表单,通过Google官方审核
  • 生成OAuth 2.0密钥并保存至本地
  • 在GCP控制台项目中启用My Business Business Information API

代码实现

import os, json
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build

CLIENT_SECRET_FILE = "E:\\catphotos\\catapultphotos\\myhappysecret.json"
SCOPES = ['https://www.googleapis.com/auth/business.manage'
,'https://www.googleapis.com/auth/mybusiness.businessinformation']
TOKEN_FILE = 'token.json'

### Generate a credentials flow
def get_credentials():
    creds = None
    # Check if the token file exists
    if os.path.exists(TOKEN_FILE):
        creds = Credentials.from_authorized_user_file(TOKEN_FILE, SCOPES)
    # If there are no valid credentials available, let the user log in
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open(TOKEN_FILE, 'w') as token:
            token.write(creds.to_json())
    return creds

### generate credentials, build service, pass account number for API
credentials = get_credentials()
service = build('mybusinessbusinessinformation','v1', credentials=credentials)
accountum = "999999999999"
output = service.accounts().locations().list(parent='accounts/%s' %accountnum, readMask='locations').execute()

错误情况

执行代码时返回400错误,提示read_mask参数无效。点击错误响应中的链接后,返回401认证失败,错误信息如下:

{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "CREDENTIALS_MISSING",
        "domain": "googleapis.com",
        "metadata": {
          "method": "google.mybusiness.businessinformation.v1.Locations.ListLocations",
          "service": "mybusinessbusinessinformation.googleapis.com"
        }
      }
    ]
  }
}

排查方向与指导

1. 修正readMask参数格式

API要求readMask必须指定具体字段路径,不能直接传locations。例如需要获取地点基础信息,可设置为:

readMask='name,title,address'

多个字段用逗号分隔,需确保字段名与API文档中定义的一致。

2. 修复变量拼写错误

代码中定义的变量是accountum,但调用时写成了accountnum,导致parent参数无效。修正为:

account_num = "999999999999"
output = service.accounts().locations().list(parent=f'accounts/{account_num}', readMask='name,title').execute()

3. 验证OAuth凭证有效性

  • 删除本地token.json,重新运行代码完成授权流程,确保生成的凭证包含有效access_token和refresh_token。
  • 确认OAuth密钥文件(myhappysecret.json)是GCP控制台中创建的桌面应用类型客户端凭证,且所属项目与启用API的项目一致。

4. 忽略链接跳转的401错误

点击错误响应中的链接出现401是因为该链接未携带你的OAuth凭证,属于正常现象,无需关注,核心问题仍为代码参数错误导致的400请求失败。

5. 确认权限与审核状态

  • 确保登录OAuth的账号拥有对应GMB账号的管理权限,且已通过Google的GMB API访问审核。
  • 在GCP控制台再次确认My Business Business Information API已正确启用,无权限配置异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 12:23:15