This tutorial shows you how to list, return, reply, and delete a review. The Google My Business API provides you with the ability to work with review data to perform the following operations:
- List all reviews.
- Get a specific review.
- Get reviews from multiple locations.
- Reply to a review.
- Delete a review reply.
Before you begin
Before you use the Google My Business API, you need to register your application and obtain OAuth 2.0 credentials. For details on how to get started with the Google My Business API, see Basic setup.
List all reviews
List all reviews of a location to audit reviews in bulk. Use the accounts.locations.reviews.list API to return all of the reviews associated with a location.
To return all reviews associated with a location, use the following:
GET https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews
The following function uses 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; }
Get a specific review
Return a specific review by name. Use the accounts.locations.reviews.get API to return a specific review associated with a location.
To return a specific review, use the following:
GET https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/{reviewId}
The following function uses 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; }
Additional data
The Java Client Library gives you access to additional field data for review instances. Use the following methods to return additional data about reviews:
getReviewId()
getComment()
getReviewer()
getStarRating()
getCreateTime()
getReviewReply()
Get reviews from multiple locations
Get reviews from multiple locations. Use the accounts.locations.batchGetReviews API to return reviews from multiple locations in a single request.
To return reviews from multiple locations, use the following:
POST https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations:batchGetReviews { "locationNames": [ string ], "pageSize": number, "pageToken": string, "orderBy": string, "ignoreRatingOnlyReviews": boolean }
Reply to a review
Reply to a specific review, or create a new reply if one doesn't exist. Use the accounts.locations.reviews.updateReply API to reply to a specific review associated with a location.
To reply to a specific review, use the following:
PUT https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/{reviewId}/reply { comment: "Thank you for visiting our business!" }
The following function uses 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; }
Delete a review reply
Delete a reply to a specific review. Use the accounts.locations.reviews.deleteReply API to delete a reply to a specific review associated with a location.
To delete a specific reply to a review, use the following:
DELETE https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/{reviewId}/reply
The following function uses 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; }