Как работать с данными отзывов

В этом руководстве рассказывается, как получить список отзывов или конкретный отзыв, а также добавить или удалить ответ. С помощью Google My Business API можно выполнить следующие действия:

Подготовка

Прежде чем начинать работу с Google My Business API, необходимо зарегистрировать свое приложение и получить учетные данные OAuth 2.0. Подробнее о начале работы с Google My Business API рассказывается в этой статье.

Как получить список отзывов

Вы можете получить сразу все отзывы для определенного адреса. Воспользуйтесь API accounts.locations.reviews.list, чтобы получить список всех отзывов, связанных с адресом.

Используйте следующий код:

HTTP
GET
https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews
Java

В следующей функции используется метод 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;
}

Как получить конкретный отзыв

Вы можете получить конкретный отзыв, указав его название. Воспользуйтесь API accounts.locations.reviews.get, чтобы получить определенный отзыв, связанный с адресом.

Используйте следующий код:

HTTP
GET
https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/{reviewId}
Java

В следующей функции используется метод 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 Client Library вы можете получить доступ к дополнительным полям с данными об отзывах. Используйте следующие методы:

  • getReviewId()
  • getComment()
  • getReviewer()
  • getStarRating()
  • getCreateTime()
  • getReviewReply()

Как получить отзывы для нескольких адресов

Вы можете получить отзывы для нескольких адресов с помощью одного запроса. Для этого воспользуйтесь API accounts.locations.batchGetReviews.

Используйте следующий код:

HTTP

POST
https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations:batchGetReviews

{
  "locationNames": [
    string
  ],
  "pageSize": number,
  "pageToken": string,
  "orderBy": string,
  "ignoreRatingOnlyReviews": boolean
}

Как ответить на отзыв

Вы можете ответить на конкретный отзыв или создать новый ответ, если он отсутствует. Чтобы ответить на конкретный отзыв, связанный с адресом, воспользуйтесь API accounts.locations.reviews.updateReply.

Используйте следующий код:

HTTP
PUT
https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/{reviewId}/reply

{
  comment: "Thank you for visiting our business!"
}
Java

В следующей функции используется метод 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;
}

Как удалить ответ на отзыв

Вы можете удалить ответ на определенный отзыв. Чтобы удалить конкретный отзыв, связанный с адресом, воспользуйтесь API accounts.locations.reviews.deleteReply.

Используйте следующий код:

HTTP
DELETE
https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/{reviewId}/reply
Java

В следующей функции используется метод 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;
}