本教學課程說明如何列出、傳回、回覆及刪除評論。您可以透過 Google My Business API 使用評論資料執行下列作業:
- 列出所有評論。
- 取得特定評論。
- 取得來自多個地點的評論。
- 回覆評論。
- 刪除評論回覆。
事前準備
使用 Google My Business API 前,您必須先註冊應用程式並取得 OAuth 2.0 憑證。如要進一步瞭解如何開始使用 Google My Business API,請參閱「基本設定」。
列出所有評論
列出某地點的所有評論,然後大量稽核評論。使用 accounts.locations.reviews.list API 即可傳回與某個地點有關的所有評論。
如要傳回與特定地點相關的所有評論,請使用以下方法:
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 API 傳回與地點相關聯的特定評論。
如要傳回特定評論,請使用以下方法:
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; }