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

使用Python调用GMB API获取评论遇AttributeError问题求助

问题与解决方案

问题描述

尝试用Python调用Google My Business(GMB)API获取评论,代码中调用service.accounts().locations().reviews()时触发错误:

AttributeError: 'Resource' object has no attribute 'locations'

同时发现官方GMB API v4的discovery文档链接失效,怀疑是API版本或文档问题。

核心原因

原Google My Business API v4已被废弃,当前官方推荐使用Google Business Profile API(v1版本),旧API的结构和文档均已停止维护,导致你调用的服务对象没有预期的locations属性。

解决步骤

  1. 切换到正确的API版本
    启用Google Cloud Console中的「Google Business Profile API」(而非旧的GMB API),确保你的项目已授权该API的访问权限。

  2. 获取正确的Discovery文档
    v1版本的Discovery文档地址为:https://mybusinessbusinessinformation.googleapis.com/$discovery/rest?version=v1
    你可以访问该链接,将内容保存为本地JSON文件(比如mybusinessbusinessinformation_v1.json),供代码调用。

  3. 调整代码适配新API
    修改sample_tools.init的参数,并修正API调用结构:

    import os
    from googleapiclient import sample_tools
    
    path = os.path.abspath('') + '\\'
    # 替换API名称、版本,并指定正确的Discovery文档
    service, flags = sample_tools.init(
        ['Google Reviews.ipynb'],
        "mybusinessbusinessinformation",  # 新API的服务名称
        "v1",  # 当前有效版本
        __doc__,
        path,
        scope="https://www.googleapis.com/auth/business.manage",
        discovery_filename="mybusinessbusinessinformation_v1.json"  # 本地保存的文档文件名
    )
    
    # 注意parent参数必须是完整格式:accounts/{account_id}/locations/{location_id}
    reviews_api = service.accounts().locations().reviews()
    request = reviews_api.list(parent="accounts/你的账号ID/locations/你的地点ID")
    # 执行请求并获取结果
    response = request.execute()
    print(response)
    

额外注意事项

  • 确保你的授权账号拥有对应Google商家账号的管理权限,否则会返回权限错误。
  • parent参数必须严格遵循accounts/{accountId}/locations/{locationId}格式,不能省略前缀路径。
  • 如果不想本地保存Discovery文档,可以去掉discovery_filename参数,让sample_tools自动从官方地址拉取文档(需确保网络可访问Google服务)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 04:32:14