使用评价数据

本教程介绍了如何列出、返回、回复和删除评价。借助 Google My Business API,您可以使用评价数据执行以下操作:

准备工作

在使用 Google My Business API 之前,您需要注册您的应用并获取 OAuth 2.0 凭据。要详细了解如何开始使用 Google My Business API,请参阅基本设置

列出所有评价

列出某个营业地点的所有评价以批量审核评价。使用 accounts.locations.reviews.list API 返回与某个营业地点关联的所有评价。

要返回与某个营业地点关联的所有评价,请使用以下命令:

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;
}

获取特定评价

按名称返回特定评价。使用 accounts.locations.reviews.get API 返回与某个营业地点关联的特定评价。

要返回特定评价,请使用以下命令:

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 客户端库,您可以查看评价实例的其他字段数据。使用以下方法可返回评价的其他相关数据:

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

获取多个营业地点的评价

获取多个营业地点的评价。使用 accounts.locations.batchGetReviews API 在单个请求中返回多个营业地点的评价。

要返回多个营业地点的评价,请使用以下命令:

HTTP

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 来回复与某个营业地点关联的特定评价。

要回复特定评价,请使用以下命令:

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;
}

删除评价回复

删除针对特定评价的回复。使用 accounts.locations.reviews.deleteReply API 删除针对与某个营业地点关联的特定评价的回复。

要删除针对某条评价的特定回复,请使用以下命令:

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;
}