クチコミのデータを管理する

このチュートリアルでは、クチコミを一覧表示、取得、返信、削除する方法を説明します。Google My Business API(GMB)ではクチコミデータに関して次のような処理が可能です。

始める前に

Google My Business API を使用するには、組み込み先のアプリケーションを事前に登録し、OAuth 2.0 の認証情報を取得する必要があります。Google My Business API の使用方法について詳しくは、基本設定をご覧ください。

すべてのクチコミの一覧を取得する

ビジネスのクチコミを一覧表示して、まとめて確認します。GMB 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;
}

特定のクチコミを取得する

名前を指定して、特定のクチコミを返します。Google My Business 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 クライアント ライブラリを使用すると、クチコミのインスタンスに関する追加のフィールド データにアクセスできます。クチコミに関する追加データを返すには、次のメソッドを使用します。

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

複数のビジネス情報からクチコミを取得する

複数のビジネス情報からクチコミを取得します。Google My Business API の accounts.locations.batchGetReviews を使用すると、1 回のリクエストで複数のビジネス情報からクチコミが返されます。

複数のビジネスからクチコミを取得するには、次のように記述します。

HTTP

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

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

クチコミに返信する

特定のクチコミへ返信するか、まだ返信がない場合は新しく返信を作成します。Google My Business 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;
}

クチコミの返信を削除する

特定のクチコミへの返信を削除します。Google My Business 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;
}