登入開始免費試用
easyCoexistence 官方 API

Python 與 Node 中的 Google Business Profile API:列出及回覆評論

要用 Python 或 Node 列出並回覆 Google 評論,請使用已獲 Basic API Access 批准的自有 Google Cloud 專案,以 OAuth 登入 business.manage 範圍,找出帳戶與地點名稱,然後呼叫 GET .../reviews 及 PUT .../reviews/{reviewId}/reply,並附上 comment。不寫代碼的話,easyCoexistence 讓 ChatGPT 或 Claude 透過其 MCP 伺服器完成同樣的工作,每則回覆都由您批准。

自有項目

Google 的 API 政策規定,以程式方式使用商家檔案的代理商與最終客戶,須使用自己的商家檔案專案。

business.manage

商家檔案 API 所需的 OAuth 範圍:https://www.googleapis.com/auth/business.manage。

0 至 300 QPM

專案在 Google 批准 Application for Basic API Access 申請前後的商家檔案 API 配額。

4,096 bytes

評論回覆 comment 欄位的最大長度。

已驗證

updateReply(建立或修改回覆的呼叫)只在地點已驗證時有效。

無沙箱

商家檔案 API 沒有沙箱;部分呼叫可傳入 validateOnly,在不作任何更改的情況下檢查請求。

為什麼需要自己的 Google Cloud 專案?

因為 Google 的商家檔案 API 政策寫明:「代理商或最終客戶自動或以程式方式使用商家檔案,須使用其自己的商家檔案專案。」讀取評論或回覆評論的代碼屬於以程式方式使用,因此必須在您擁有並經 Google 批准的 Cloud 專案上運行,不能透過其他公司的專案或 API。Google 按專案授予存取權,之後任何可存取該專案的用戶都可以呼叫 API。

呼叫 Google API 前需要準備什麼?

四項,各須在您自己的 Google 帳戶與專案中設定:

  • 已獲 Basic API Access 批准的 Cloud 專案,須經 Google 的 GBP API 聯絡表格提出申請,而申請電郵必須是商家檔案的擁有者或管理員。在獲批之前,配額為 0 QPM。
  • 在 Cloud 控制台啟用商家檔案 API。本代碼使用三項:保存評論的 Google My Business API、My Business Account Management API 及 My Business Business Information API。
  • 類型為桌面應用程式的 OAuth 用戶端,以 credentials.json 名稱存放於腳本旁。若應用程式的對象為外部且處於測試模式,請將您的 Google 帳戶加為測試用戶。
  • 一個擁有或管理該地點的 Google 帳戶,於瀏覽器開啟時用作登入。

哪些 Google 端點可以列出評論及發表回覆?

前兩個呼叫用來找出 Google 所需的資源名稱 accounts/{accountId} 與 locations/{locationId},之後評論呼叫便在 accounts/{accountId}/locations/{locationId} 下運作。所有呼叫都需要 business.manage 範圍。v4 呼叫背後的 Google My Business API,要待 Google 批准您的存取申請後,才會在 Cloud 控制台中出現。

accounts.listGET mybusinessaccountmanagement.googleapis.com/v1/accounts: accounts you own or manage, your personal account first
accounts.locations.listGET mybusinessbusinessinformation.googleapis.com/v1/{account}/locations, with readMask required
accounts.locations.reviews.listGET mybusiness.googleapis.com/v4/{account}/{location}/reviews: up to 50 a page, updateTime desc by default
accounts.locations.reviews.updateReplyPUT .../reviews/{reviewId}/reply with a comment: creates the reply, or updates the one there
accounts.locations.batchGetReviewsPOST .../v4/{account}/locations:batchGetReviews: up to 50 verified locations a call

如何用 Python 列出評論及回覆?

使用 Google 的驗證程式庫及純 HTTPS 呼叫。腳本會開啟瀏覽器進行 Google 登入,列出每則評論的 id 與星級評分,然後詢問要回覆哪一則及回覆內容,因此在真人輸入之前,不會發出任何內容。

reviews.py: Google's API with OAuth
# pip install google-auth-oauthlib
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import AuthorizedSession

SCOPES = ["https://www.googleapis.com/auth/business.manage"]
flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
google = AuthorizedSession(flow.run_local_server(port=0))

# accounts[0] is your personal account; pick another from the list if the location sits elsewhere
account = google.get("https://mybusinessaccountmanagement.googleapis.com/v1/accounts").json()["accounts"][0]["name"]
location = google.get(
    f"https://mybusinessbusinessinformation.googleapis.com/v1/{account}/locations",
    params={"readMask": "name,title"},
).json()["locations"][0]["name"]
reviews_url = f"https://mybusiness.googleapis.com/v4/{account}/{location}/reviews"

for r in google.get(reviews_url).json().get("reviews", []):
    print(r["reviewId"], r["starRating"], r.get("comment", ""), "(replied)" if "reviewReply" in r else "")

review_id = input("Review id to answer: ")
text = input("Reply, as the owner approved it: ")
google.put(f"{reviews_url}/{review_id}/reply", json={"comment": text}).raise_for_status()

Node 中怎樣做同樣的事?

使用 Google 的本機驗證套件,它會開啟同一個瀏覽器登入,並返回一個已授權的用戶端,其 request 方法可呼叫任何端點。腳本會列出評論,然後如 Python 版本一樣,要求輸入評論 id 及已批准的內容。請將其存為 .mjs 檔案,以便頂層 await 可以運作。

reviews.mjs: Google's API with OAuth
// npm install @google-cloud/local-auth   then: node reviews.mjs
import path from "node:path";
import readline from "node:readline/promises";
import { authenticate } from "@google-cloud/local-auth";

const google = await authenticate({
  scopes: ["https://www.googleapis.com/auth/business.manage"],
  keyfilePath: path.resolve("credentials.json"),
});
const get = async (url) => (await google.request({ url })).data;

// accounts[0] is your personal account; pick another if the location sits elsewhere
const { accounts } = await get("https://mybusinessaccountmanagement.googleapis.com/v1/accounts");
const account = accounts[0].name;
const { locations } = await get(
  `https://mybusinessbusinessinformation.googleapis.com/v1/${account}/locations?readMask=name,title`
);
const reviewsUrl = `https://mybusiness.googleapis.com/v4/${account}/${locations[0].name}/reviews`;

const { reviews = [] } = await get(reviewsUrl);
for (const r of reviews) console.log(r.reviewId, r.starRating, r.comment ?? "", r.reviewReply ? "(replied)" : "");

const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const reviewId = await rl.question("Review id to answer: ");
const comment = await rl.question("Reply, as the owner approved it: ");
rl.close();
await google.request({ url: `${reviewsUrl}/${reviewId}/reply`, method: "PUT", data: { comment } });

為什麼代碼要等真人確認才回覆?

因為 Google 的政策規定:「未經用戶事先具體及明確的同意,不得自動化或觸發評論回覆、問答、商家資訊建立、商家資訊編輯或其他操作」,而代表客戶回覆的代理商,必須先取得客戶的授權。另外,回覆是公開的:Google 批准後,它會以商家的名義顯示於評論之下,不附任何個人姓名,而評論者會收到通知。

不寫代碼怎樣完成同樣的工作?

透過 easyCoexistence 登入並提出要求。在其控制台中選擇「新增連接」,然後選擇 Google Business,以該商家檔案的擁有者或管理員身分用 Google 登入,再將 https://easycoexistence.com/api/mcp 加入 ChatGPT 或 Claude。請求評論但不要回覆:助手會呼叫 list_google_reviews,為每則評論草擬回覆,向您展示每份草稿,並只對您批准的回覆呼叫 reply_to_google_review。delete_google_review_reply 會撤下回覆,評論本身則保留。若要不必每次詢問即自動回覆新評論,請在控制台開啟評論自動化:它會以固定文字或您自己的 AI 回覆,或將每則新評論傳送到您的 Webhook,並發出它所回傳的回覆。

設定方法

  1. 建立項目建立 Google Cloud 項目,記下項目編號,然後提交 Google 的 GBP API 聯絡表格,並在 Basic API Access 中選擇 Application。
  2. 啟用 API當配額顯示 300 QPM 後,在 Cloud 控制台啟用 Business Profile API。
  3. 建立 OAuth 用戶端建立 Desktop app 類型的 OAuth 用戶端,並將其 JSON 儲存為 credentials.json,放在腳本旁。
  4. 執行並登入執行 Python 或 Node 腳本,並以擁有或管理該地點的 Google 帳戶登入。
  5. 回覆一則評論從列表選取一個評論 id,輸入擁有者已批准的回覆。

常見錯誤

  • 在 0 QPM 時申請提高配額。Google 的限制頁面說明配額為 0 即表示尚未授予存取權,因此應改為提交 Application for Basic API Access。
  • 呼叫地點時省略 readMask。Business Information API 將其列為必填,因此至少要傳送 readMask=name,title。
  • 在真實評論上測試回覆呼叫。商家檔案 API 沒有沙箱,因此測試回覆就是商家以真實、公開的身分發出的回覆。
  • 以字元計算 4,096 位元組的回覆上限。重音字母與表情符號各佔多個位元組,因此看似很短的回覆仍可能被拒絕。
使用 EasyCoexistence 完成這項設定

easyCoexistence 是達成同樣工作的無代碼途徑:以 Google 登入連接地點,將其 MCP 伺服器加入 ChatGPT 或 Claude,然後請求不附回覆的評論。助手會草擬回覆,只發出您批准的回覆;評論由 Google 即時讀取,不會儲存。您也可以在其控制台開啟評論自動化,讓它自行回覆新評論。每個連接每月由 US$ 9 降至 US$ 0.75,連接越多越便宜,首 3 天免費試用。

將 Google 商家檔案連接至 ChatGPT 及 Claude

常見問題

為什麼腳本會收到 403 錯誤?

Google 的設定指南列出一個原因:當 Google Workspace 帳戶關閉商家檔案時,呼叫會以 403 PERMISSION DENIED 失敗。另請檢查腳本所呼叫的三項 API 是否已在您的專案中啟用。

如何列出超過 50 則評論?

跟隨分頁權杖。reviews.list 每頁最多返回 50 則評論,若仍有更多,每個回應會附帶 nextPageToken,於下一次請求中以 pageToken 傳送。

如何測試而不影響真實的商家檔案?

Google 沒有測試商家資訊:其 API 常見問題指出,假的商家資訊通常會被標記並暫停。它建議使用貴公司總部的商家資訊、在非辦公時間處理的客戶商家資訊(不改動其名稱、地址或類別),或模擬 API 回應。

如何知道回覆已生效?

再次列出評論,並讀取回覆的 reviewReplyState:PENDING、APPROVED 或 REJECTED,被拒絕的會附有 policyViolation。reviews.get 不包含該狀態,而 Google 的檢查通常需時最多 10 分鐘,有時達 30 天。

繼續閱讀

兩分鐘內連接您的第一個頻道。

無需等待平台審核。連接 WhatsApp、Instagram、TikTok 或其他頻道,然後請您的 AI 發佈貼文、回覆訊息或匯報數據,並看着它運作。

  • 免費試用 3 天
  • 隨時取消
  • 只用官方 API
  • 24/7 監察

驗證日期