本教學課程說明如何列出、傳回、回覆及刪除評論。 Google My Business API 可讓您將評論資料用於 執行下列作業:
事前準備
使用 Google My Business API 之前,請務必先註冊 並取得 OAuth 2.0 憑證。進一步瞭解如何開始使用 與 Google My Business API 搭配使用,請參閱 基本設定。
列出所有評論
列出地點的所有評論,以便大量稽核評論。使用 accounts.locations.reviews.list 來傳回與地點相關的所有評論。
如要傳回與某個地點相關的所有評論,請使用以下方法:
GET https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews
以下函式使用 Mybusiness.Accounts.Locations.Reviews.List
。
/** * Returns a list of reviews. * @param locationName Name of the location to retrieve reviews for. * @return List<Reviews> A list of reviews. * @throws Exception */ public static List<Review> listReviews(String locationName) throws Exception { Mybusiness.Accounts.Locations.Reviews.List reviewsList = mybusiness.accounts().locations().reviews().list(locationName); ListReviewsResponse response = accountsList.execute(); List<Reviews> reviews = response.getReviews(); for (Reviews review : reviews) { System.out.println(review.toPrettyString()); } return reviews; }
取得特定評論
依名稱傳回特定評論。使用 accounts.locations.reviews.get 用來傳回與地點相關的特定評論。
如要傳回特定評論,請使用以下方式:
GET https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/{reviewId}
以下函式使用 Mybusiness.Accounts.Locations.Reviews.Get
。
/** * Demonstrates getting a review by name. * @param reviewName The name (resource path) of the review to retrieve. * @return Account The requested review. */ private static Review getReview(String reviewName) throws Exception { Mybusiness.Accounts.Locations.Reviews.Get review = mybusiness.accounts().locations().reviews().get(reviewName); Review response = review.execute(); return response; }
額外資料
Java 用戶端程式庫可讓您存取其他欄位資料以供審查 執行個體。請使用下列方法傳回評論的其他相關資料:
getReviewId()
getComment()
getReviewer()
getStarRating()
getCreateTime()
getReviewReply()
取得多個地點的評論
取得來自多個地點的評論。使用 accounts.locations.batchGetReviews API,透過單一要求傳回多個地點的評論。
如要傳回多個地點的評論,請使用下列方式:
POST https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations:batchGetReviews { "locationNames": [ string ], "pageSize": number, "pageToken": string, "orderBy": string, "ignoreRatingOnlyReviews": boolean }
回覆評論
回覆特定評論,如果沒有回覆,請建立新回覆。使用 accounts.locations.reviews.updateReply 提供 API 以回覆與營業地點相關的特定評論。
如要回覆特定評論,請使用下列資訊:
PUT https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/{reviewId}/reply { comment: "Thank you for visiting our business!" }
以下函式使用 Mybusiness.accounts.locations.reviews.reply
。
/* * Updates the reply for a location review. * If a review does not exist, creates a new one. * @param reviewName Name of the review being responded to. * @param comment A string containing the review response body. * @throws IOException */ private static Reply reply(String reviewName, String comment) throws IOException { MyBusiness.Accounts.Locations.Reviews.Reply reply = mybusiness().accounts().locations().reviews().reply(reviewName, comment); Reply response = reviewReply.execute(); return response; }
刪除評論回覆
刪除特定評論的回覆。使用 accounts.locations.reviews.deleteReply 提供相關 API,用於刪除與營業地點相關的特定評論回覆。
如要刪除某則評論的特定回覆,請使用以下方式:
DELETE https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/{reviewId}/reply
以下函式使用 Mybusiness.Accounts.Locations.Reviews.DeleteReply
。
/** * Demonstrates deleting a review reply by name. * @param reviewName The name (resource path) of the review reply to delete. * @return Account The requested review. */ private static DeleteReply deleteReply(String reviewName) throws Exception { Mybusiness.Accounts.Locations.Reviews.DeleteReply toDelete = mybusiness.accounts().locations().reviews().deleteReply(reviewName); DeleteReply response = toDelete.execute(); return response; }