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

Getting Reviews for locations with review count > 200

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. Please let me know what I am doing wrong.

Below is the code:

 

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();
        }
    }
1 Expert replyverified_user
Marked as Best Answer.
Solution
Accepted by topic author Sathya K
December 2016

Re: Getting Reviews for locations with review count > 200

[ Edited ]
Google Employee
# 2
Google Employee

Hi @Sathya K

 

We have answered your question already in another thread. Please check out this answer.

 

Thanks,

The Google My Business API team