In the last blog, I talked about how to get places’ reviews on Google by Place API, but we can get only 5 reviews at most by “Place Details”. If you want to extract all reviews of each location, you can use “Google My Business API”. In this blog, I will talk about how to get places’ reviews on Google by Google My Business API with the following points:
- What is Google My Business (GMB)?
- Prerequisites
- Using OAuth 2.0 to Access Google APIs
- How to perform OAuth 2.0 using the Curl CLI?
- How to get information by Google My Business API?
- How to realise the request by Python?
What is Google My Business (GMB)?
Google My Business is an Internet-based service for business owners which is operated by Google. The network launched in June 2014 as a way of giving business owners more control of what shows up in the search results when someone searches a given business name.
Prerequisites
Before starting, you should complete these prerequisites. After completing them, you can enable the Google My Business API, request an OAuth 2.0 client ID; then you can test your API with OAuth 2.0 Playground.
Using OAuth 2.0 to Access Google APIs
Concerning how to use OAuth 2.0 to access Google APIs, we can summarize as the following four points:
- Obtain OAuth 2.0 credentials from the Google API Console.
- Obtain an access token from the Google Authorization Server.
- Send the access token to an API.
- Refresh the access token, if necessary.
The authorization sequence begins when your application redirects a browser to a Google URL; the URL includes query parameters that indicate the type of access being requested. Google handles the user authentication, session selection, and user consent. The result is an authorization code, which the application can exchange for an access token and a refresh token.
The application should store the refresh token for future use and use the access token to access a Google API. Once the access token expires, the application uses the refresh token to obtain a new one.
How to perform OAuth 2.0 using the Curl CLI?
Now we understand how to access Google APIs with OAuth 2.0, let’s see how to perform its logic by curl. Inspired by John Hanley’s blog, we have the following ways for Windows Command Prompt users but should be easily adaptable to Linux and Mac also.
In the following example, the Scope is business.manage
. Modify to use the
scopes that you want to test with.
Steps:
- Copy the following statements to a Windows batch file.
- Modify to fit your environment.
- Modify the script for the browser that you want to use.
- Run the batch file.
- A browser will be launched.
- The browser will go to https://developers.google.com/oauthplayground where you can complete the Google OAuth 2.0 authentication.
- Once complete a code will be displayed in the browser window.
- Copy this code from the browser window and paste into the command prompt window.
- The script will complete the OAuth 2.0 code exchange for a Token.
- The Token will be displayed in the command prompt.
- The returned Token contains an Access Token that can be used in more curl commands.
The output looks like this:
I copied access_token
’s value and saved it in a .properties
file.
How to get information by Google My Business API?
With access_token
, we can finally send our requests by GMB API. In my case,
I need to extract clients’ reviews for each location; in order to achieve it,
we firstly need accounts
data and locations
data with the following steps:
- Get
accounts
data - Get
locations
data withaccounts
data - Get
reviews
data withlocations
data
Get “accounts” data
In the first step, we need to get the specified account with HTTP request:
The output should be similar as:
What we need is the value of name
, it’s like “accounts/0123456789”.
Get “locations” data with “accounts” data
With the value of accounts
, now we can extract the locations which are
contained in the account with HTTP request.
The output should be similar as:
What we need is the value of name
, it’s like
“accounts/0123456789/locations/9876543210”.
By the way, we can get only 100 locations as most per request. If it has more
than 100 locations, it will return a nextPageToken
as well to extract other
locations. However, it seems that it doesn’t work properly,
so we can send the request with the postal_code
filter, like
?filter=address.postal_code="75014"
, then we can get all locations of quarter
“75014”.
Get “reviews” data with “locations” data
With the value of locations
, now we can extract the their reviews and notes
with HTTP request.
The output should be similar as:
What we need is the value of starRating
and comment
, the value of
starRating
is one of “ONE”, “TWO”, “THREE”, “FOUR” and “FIVE”.
How to realise the request by Python?
If you want to achieve the extraction above by Python, we need to import
requests
module, then create a dictionary to specify the HTTP headers before
sending requests by Python.
Get “accounts” data
What we need is the value of name
, it’s like “accounts/0123456789”.
Get “locations” data with “accounts” data
We assign “accounts/0123456789” as input accounts
of search_locations()
,
then we will get locations’ information.
Get “reviews” data with “locations” data
Reference
- “Google My Business”, www.google.com. [Online]. Available: https://www.google.com/intl/en_us/business/
- “Google My Business API”, www.google.com. [Online]. Available: https://developers.google.com/my-business/content/overview
- “Google My Business API Reference”, www.google.com. [Online]. Available: https://developers.google.com/my-business/reference/rest
- “Reviews API (nextPageToken) not working properly “, support.google.com. [Online]. Available: https://support.google.com/business/thread/2096654?hl=en