My Business
5.1K members online now
5.1K members online now
For developers who are using the Google My Business API to manage locations
Guide Me
star_border
Reply

/Reviews errors when accessing locations with reviews more than 200

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

I am trying to fetch all the reviews using GMB API. For this business, they have around 1200 reviews. when i execute this code i get reviews inconsistently. sometimes 200, sometimes 400, sometimes 800 and finally after few runs i got all the reviews 1294.  Could you please let me know if there is any kind of time out ? or if I am doing something wrong ?

2 Expert replyverified_user

/Reviews errors when accessing locations with reviews more than 200

Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭

unfortunately anytime i post the code here.. it is getting deleted.

Re: /Reviews errors when accessing locations with reviews more than 200

Google Employee
# 3
Google Employee

Hi @Sathya K,

 

I've copied and pasted the source code you provided in an email below in this post. Please check and report back to me if the source code I entered in this post is incorrect.


Sample code provided from client's email:

 

public static void getReviews(String pageId, String token, String secret) {
        HttpTransport httpTransport;
        try {
            token = "---access---token---";
            secret = "---secret---key----";
            pageId = "accounts/{accountId}/locations/{locationID}";
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();
            Credential credential = buildCredential(token, secret);
            mybusiness = new Mybusiness.Builder(httpTransport, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME).build();            
// reviews list request            com.google.api.services.mybusiness.v3.Mybusiness.Accounts.Locations.Reviews.List reviewsListRequest = mybusiness
                .accounts().locations().reviews().list(pageId);
// pageSize 200 - pagination
            reviewsListRequest.setPageSize(200);
            ListReviewsResponse reviewResponse = reviewsListRequest.execute();
            if (reviewResponse != null) {
                System.out.printf("reviews fetched for pageId - %s is %s with pageToken %s \n", pageId,
                    reviewResponse.getTotalReviewCount(), reviewResponse.getNextPageToken());
                System.out.println("review count " + reviewResponse.getReviews().size());
                List<Review> reviews = new ArrayList<Review>();
                if (reviewResponse.getReviews() != null) {
                    ListIterator<Review> iterator = reviewResponse.getReviews().listIterator();
                    while (iterator.hasNext()) {
                        reviews.add(iterator.next());
                    }
                }
                while (reviewResponse.getNextPageToken() != null) {
                    reviewsListRequest.setPageToken(reviewResponse.getNextPageToken());
                    reviewResponse = reviewsListRequest.execute();
                    if (reviewResponse != null && reviewResponse.getReviews() != null) {
                        ListIterator<Review> iterator = reviewResponse.getReviews().listIterator();
                        while (iterator.hasNext()) {
                            reviews.add(iterator.next());
                        }
                    }
                    System.out.printf("reviews fetched for pageId - %s is %s with pageToken %s\n", pageId,
                        reviewResponse.getTotalReviewCount(), reviewsListRequest.getPageToken());
                    if (reviewResponse.getReviews() != null)
                        System.out.println("response reviews size " + reviewResponse.getReviews().size());
                    System.out.println("review count " + reviews.size());
                }
                System.out.println("total reviews collected " + reviews.size());
            } else {
                System.out.println("0 reviews fetched for pageId: " + pageId);
            }
            //            System.out.println("reviews....");
            //            for (Review review : reviews) {
            //                System.out.println(review.toPrettyString());
            //            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 

Thanks,

Terry

/Reviews errors when accessing locations with reviews more than 200

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭

Thanks @Terry.

 

This looks fine. Can you please confirm the implementation is fine. Also, any kind of QPS we need to be aware of?

Re: /Reviews errors when accessing locations with reviews more than 200

Google Employee
# 5
Google Employee

Hi @Sathya K,

 

Judging from the source code you provided, what happened was that you created a list of reviews in the following line of code and counted the number of reviews in that list as you paged through and added the paginated list of reviews for the specified location to your own list by sending the GET request with the pageSize and pageToken query parameters:

List<Review> reviews = new ArrayList<Review>();

 

Therefore, it makes sense that the size of your own list of reviews incremented by the maximum pageSize you set that’s 200 as indicated in the following line of code:

reviewsListRequest.setPageSize(200);

 

That’s the reason why the size of your own review list went from 200 to 400, and eventually amounted to the totalReviewCount, which is constant and should be shown in the following line of code:

System.out.printf("reviews fetched for pageId - %s is %s with pageToken %s\n", pageId, reviewResponse.getTotalReviewCount(), reviewsListRequest.getPageToken());

 

Your implementation is okay, but I suggest you not create that extra list of reviews unless you are saving the reviews for caching, and check that the isVerified boolean field of the LocationState object in the specified location is true since any review operation is only valid if the location is verified. Here is a code snippet for listing all paginated reviews for the specified location by including the pageSize and pageToken query parameters with the Java client library:

 public static void listPaginatedReviews(Location location) throws Exception {

   if (location.getLocationState().getIsVerified() != null

       && location.getLocationState().getIsVerified()) {

     // Specifies the pageSize and pageToken query parameters to list paginated reviews

     Mybusiness.Accounts.Locations.Reviews.List listReviews =

         mybusiness.accounts().locations().reviews().list(location.getName()).setPageSize(200);

     listReviews.getRequestHeaders().set("X-GOOG-API-FORMAT-VERSION", 2);

     ListReviewsResponse response = listReviews.execute();

 

     if (response != null) {

       System.out.println(response.toPrettyString());

       while (response.getNextPageToken() != null) {

         listReviews =

             mybusiness

                 .accounts()

                 .locations()

                 .reviews()

                 .list(location.getName())

                 .setPageSize(200)

                 .setPageToken(response.getNextPageToken());

         listReviews.getRequestHeaders().set("X-GOOG-API-FORMAT-VERSION", 2);

         response = listReviews.execute();

         System.out.println(response.toPrettyString());

       }

       // Gets the total number of reviews for this location.

       System.out.println("\nTotal Review Count: " + response.getTotalReviewCount());

     } else {

       System.out.printf("Location '%s' has no reviews.", location.getName());

     }

   } else {

     System.out.printf("The specified location '%s' is not verified.", location.getName());

   }

 }

 

In addition, as I’ve explained, your own review list size increments do not have anything to do with the API Usage Limits of 5 queries per second (QPS) per project, which we don’t currently offer any options to increase.

 

Thanks,

Terry

/Reviews errors when accessing locations with reviews more than 200

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭

So you just post this in <script> tags and change out the token, secret, and pageid (account name & locationid)? And if so, what should the account name be and is the locationid equal to the placeid for the location of the business on Google Maps? 

Re: /Reviews errors when accessing locations with reviews more than 200

Google Employee
# 7
Google Employee

Hi @OAVHome L,

 

No, location_id is not same as placeid for the location of the business on Google Map.

 

Judging from your email address on this board, your company may not have a whitelisted project to access the Google My Business API. So before you use any Google My Business API, you need to have a whitelisted project to access them.

 

We suggest you please check out the Prerequisites page of our documentation website to learn more about requesting access to the Google My Business API.

 

Thanks,

The Google My Business API team