I recently tried listing all the reviews using the Google My Business API, and it is incredibly tiring. Due to the deprecation of earlier APIs and the abundance of outdated information on Stack Overflow, Medium etc., I spent quite a lot of time going through the internet for 'Pulling Review from Google My business with python', 'How to get places' reviews on Google by Google My Business API?' and all other permutations and combinations of it. Finally, I thought about going through the process myself and sharing the working Python code.
Get the code here.
Before you pick up the code and try to run it to obtain the google reviews, there are a few things to take care of. You require a gmail account with the correct permissions, access to the GMB API and some other APIs and a few other minor details which are to be taken into account. Hence, if you don't know what those stages are, just go to this page to get to know the prerequisites. For step by step process setup the Google My Business API visit the basic setup page and you'll be good to go. But if you are aware of them and have taken the necessary action, then please continue.
IDE used: Spyder
Step 1. Installing the python libraries.
Step 2. Obtaining the credentials.
Now this is where the most important thing happens. This will only work if you’ve completed the prerequisites. If not please visit the prerequisite link and complete them first.
The code is related to authorization and authentication of an application or script that interacts with Google APIs. It uses the Google OAuth 2.0 protocol to authenticate the user and grant permissions to the application to access their Google account data.
The first line initializes a variable creds to None. Then it checks whether there exists a file named "token.json" in the current directory. If it does, it loads the previously stored credentials from that file, which were obtained by the user granting permission to the application in the past. If the stored credentials are not valid or do not exist, the script will start a new authorization flow to obtain new credentials. First, it checks if the stored credentials have expired, and if they have a refresh_token. If so, it uses the refresh token to obtain new access and refresh tokens without requiring the user to manually authorize the application again. If the stored credentials do not have a refresh token, the script starts a new authorization flow to obtain new credentials from the user. Finally, the obtained credentials are saved to "token.json" file for later use.
Step 3. Obtaining the account and locations.
The code creates service objects for two different Google My Business APIs using the build() function and creds credentials obtained from previous code.
In the first block, it uses the "mybusinessaccountmanagement" API to retrieve a list of accounts associated with the authorized user by calling the accounts().list().execute() method. It assigns the name of the first account in the list to the variable account_name.
In the second block, it uses the "mybusinessbusinessinformation" API to retrieve a list of locations for the account with the parent OW and readMask Name and title. The retrieved information is stored in the loc variable.
The code then extracts the values from the loc dictionary and puts them in a list called location_list. I stored all the locations in a list to iterate over the locations are pull reviews for all of them in a single loop.
Step 4: Obtaining the reviews for all locations with nextpagetoken handling implemented.
This code defines a function called "get_reviews" that takes several parameters, including the account ID, location ID, title, page size, page token, and order by. Inside the function, it first retrieves the credentials using the "creds" variable.
Then, it creates a request header with the authorization token and sends a GET request to the Google My Business API to retrieve review data for the specified account and location. The returned data is stored in the "review_data" variable.
Next, it checks the total number of reviews and assigns it to the "count" variable. If there are no reviews, the count is set to 0. The review data is also written to a JSON file with the title specified in the "current_title" variable.
If there are more than 50 reviews, the code retrieves the next page of reviews using the "nextPageToken" parameter and writes the data to additional JSON files, each with a different file name. Finally, the function prints the API request result using the "print" function.
Step 5. Combining the results of all the location and creating a single CSV file.
This part of code will add a column with the location title and then concatenate all the json files into a single csv file with all the data and save it in the current directory.
The code is almost 95% ready to copy-paste and run, but if there are some errors there just might be some minor ones which can be easily solved through simple googling.