Google Business API:配置Pub Sub更新通知,订阅商家新评论通知
实现Google Business Profile评论更新通知的完整配置指南
没问题,我来帮你把这个评论订阅功能补全并梳理清楚所有关键配置步骤,确保你能顺利收到商家的新评论通知。
一、前置准备工作
在写代码之前,必须先完成这些Cloud平台的配置:
- 启用Google Business Profile API和Cloud Pub/Sub API:登录Google Cloud控制台,在API库中搜索并启用这两个服务。
- 创建目标Pub/Sub主题:就是你代码里的
topicName对应的主题,确保主题所在项目和GMB API使用的项目一致。 - 配置Pub/Sub主题权限:给Google Business Profile的专用服务账号
mybusiness-api-pubsub@system.gserviceaccount.com授予Pub/Sub Publisher角色,否则API无法向你的主题推送消息。操作路径:Cloud Console -> Pub/Sub -> 你的主题 -> 权限 -> 添加成员,输入上述账号,选择对应角色。 - 确保代码的认证正常:不管用OAuth2还是服务账号认证,要保证你的
GMBFactory能生成有权限调用GMB API的MyBusiness实例。
二、补全后的完整代码示例
你原来的代码只到了循环账号的部分,下面是补全后的逻辑,包含获取商家位置并设置评论通知的完整流程:
private static final String topicName = "projects/myproject-12345/topics/business-review"; public void setupReviewNotifications() throws IOException { MyBusiness myBusiness = GMBFactory.getMyBusiness(); // 获取所有关联的GMB账号 MyBusiness.Accounts.List accountsList = myBusiness.accounts().list(); ListAccountsResponse accountsResponse = accountsList.execute(); List<Account> accounts = accountsResponse.getAccounts(); // 处理分页(如果账号数量多,需要循环获取所有结果) while (accountsResponse.getNextPageToken() != null) { accountsList.setPageToken(accountsResponse.getNextPageToken()); accountsResponse = accountsList.execute(); accounts.addAll(accountsResponse.getAccounts()); } for (Account account : accounts) { String accountName = account.getName(); // 获取该账号下的所有商家位置 MyBusiness.Accounts.Locations.List locationsList = myBusiness.accounts().locations().list(accountName); ListLocationsResponse locationsResponse = locationsList.execute(); List<Location> locations = locationsResponse.getLocations(); // 同样处理位置的分页 while (locationsResponse.getNextPageToken() != null) { locationsList.setPageToken(locationsResponse.getNextPageToken()); locationsResponse = locationsList.execute(); locations.addAll(locationsResponse.getLocations()); } for (Location location : locations) { String locationName = location.getName(); // 构建评论更新的通知设置 NotificationSetting notificationSetting = new NotificationSetting() .setTopicName(topicName) .setEventType("REVIEW_UPDATED"); // 监听评论新增/更新事件 try { // 创建或更新该位置的通知设置 MyBusiness.Accounts.Locations.NotificationSettings.Create createRequest = myBusiness.accounts().locations().notificationSettings().create(locationName, notificationSetting); NotificationSetting createdSetting = createRequest.execute(); System.out.printf("成功为位置 [%s] 配置评论通知,关联主题:%s%n", locationName, createdSetting.getTopicName()); } catch (GoogleJsonResponseException e) { // 处理API调用错误,比如权限不足、位置不存在等 System.err.printf("配置位置 [%s] 通知失败:%s%n", locationName, e.getDetails().getMessage()); } } } }
三、关键注意事项
- 事件类型选择:除了
REVIEW_UPDATED,你还可以根据需求选择其他事件类型,比如REVIEW_REPLIED(评论被回复时触发),具体支持的类型可以参考官方API文档中的枚举值。 - 分页处理:如果你的账号下有大量商家位置,一定要处理分页逻辑,否则会漏掉部分位置的配置。
- 错误处理:实际生产环境中要完善异常捕获,比如处理认证失败、API配额超限、权限不足等情况。
四、验证配置是否生效
配置完成后,可以通过以下步骤验证:
- 在Google Business Profile后台给某个测试商家添加一条新评论。
- 登录Cloud Console,进入你的Pub/Sub主题,创建一个临时订阅。
- 使用订阅的“拉取消息”功能,查看是否收到了评论更新的通知消息。
- 可以在Cloud Console的API和服务 -> 日志中查看GMB API的调用记录,确认没有错误。
内容的提问来源于stack exchange,提问作者Aakash
相关产品推荐
相关产品推荐

