本教學課程將說明如何列出、退回、回覆及刪除評論。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; }