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

如何在Python中结合Google My Business API V1与V4获取评论?

解决方案:在Python中调用Google My Business V4评论API

由于Google My Business的评论功能尚未迁移至V1 API,你需要单独构建V4版本的服务实例,或直接发送HTTP请求来调用评论接口,以下是两种可行方案:

方案一:使用googleapiclient构建V4服务实例

复用已有认证流程,手动构建V4版本的服务对象,直接调用评论方法:

from googleapiclient import sample_tools
from googleapiclient.discovery import build
from googleapiclient.http import build_http
import json
import sys

argv = sys.argv
__doc__ = ""  # 可替换为你的脚本文档说明(可选)
__file__ = __file__

# 初始化V1服务并获取认证上下文
_, flags = sample_tools.init(
    argv,
    "mybusinessaccountmanagement",
    "v1",
    __doc__,
    __file__,
    scope="https://www.googleapis.com/auth/business.manage"
)

# 构建V4版本的服务实例(手动指定发现文档地址)
http = build_http()
my_business_v4 = build(
    "mybusinessaccountmanagement",
    "v4",
    http=http,
    discoveryServiceUrl="https://mybusinessaccountmanagement.googleapis.com/$discovery/rest?version=v4"
)

# 调用评论接口,替换为你的账户ID和位置ID
account_id = "你的账户ID"
location_id = "你的位置ID"
reviews_response = my_business_v4.accounts().locations().reviews().list(
    parent=f"accounts/{account_id}/locations/{location_id}"
).execute()

print("获取到的评论:")
print(json.dumps(reviews_response, indent=2))

方案二:直接发送HTTP请求

如果V4服务的客户端方法调用有问题,可直接通过认证后的HTTP客户端发送请求:

from googleapiclient import sample_tools
from googleapiclient.http import build_http
import json
import sys

argv = sys.argv
__doc__ = ""  # 可替换为你的脚本文档说明(可选)
__file__ = __file__

# 初始化获取认证后的HTTP客户端
_, flags = sample_tools.init(
    argv,
    "mybusinessaccountmanagement",
    "v1",
    __doc__,
    __file__,
    scope="https://www.googleapis.com/auth/business.manage"
)

http = build_http()
# 触发认证,获取带Authorization的请求头
headers = {}
http.request("GET", "https://www.googleapis.com/oauth2/v3/userinfo", headers=headers)

# 发送请求到V4评论接口
account_id = "你的账户ID"
location_id = "你的位置ID"
api_url = f"https://mybusinessaccountmanagement.googleapis.com/v4/accounts/{account_id}/locations/{location_id}/reviews"
response, content = http.request(api_url, method="GET", headers=headers)

if response.status == 200:
    reviews_data = json.loads(content)
    print("获取到的评论:")
    print(json.dumps(reviews_data, indent=2))
else:
    print(f"请求失败,状态码:{response.status}")
    print(f"错误信息:{content.decode('utf-8')}")

注意事项

  • 替换代码中的你的账户ID和你的位置ID:可通过V1 API的accounts().list()和accounts().locations().list()接口获取
  • 保持权限范围https://www.googleapis.com/auth/business.manage不变,该权限同时支持V1和V4接口的访问
  • 尽管V4 API标记为弃用,但目前评论功能仍未迁移至V1,因此该方案是当前唯一可行的方式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 07:53:36