André Pletschette
André Pletschette

Reputation: 302

Getting Google site reviews with Python

I need to be able to get Google site reviews by their API do put them in our data warehouse.

I tried with the following code:

from googleapiclient.discovery import build

API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxx'
service = build('**mybusiness**', 'v4', developerKey=API_KEY)

location_id = 'locations/xxxxxxxxxxxx'

# Request reviews
reviews = service.accounts().locations().reviews().list(parent=location_id).execute()
# Print review data
for review in reviews['reviews']:
    print(f"Review: {review['review']}")
    print(f"Rating: {review['starRating']}")
    print('---')

But I get an error that mybusiness v4 does not exist:

Traceback (most recent call last):
  File "<PythonBin>\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<PythonBin>\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "<PythonBin>\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "<PythonBin>\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main        
    run()
  File "<PythonBin>\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file    
    runpy.run_path(target, run_name="__main__")
  File "<PythonBin>\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<PythonBin>\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "<PythonBin>\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "C:\src\DownloadGoogleReviewToDWH - test 2.py", line 4, in <module>
    service = build('**mybusiness**', 'v4', developerKey=API_KEY)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<PythonBin>Python312\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "<PythonBin>Python312\site-packages\googleapiclient\discovery.py", line 304, in build  
    content = _retrieve_discovery_doc(
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "<PythonBin>Python312\site-packages\googleapiclient\discovery.py", line 417, in _retrieve_discovery_doc
    content = discovery_cache.get_static_doc(serviceName, version)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<PythonBin>Python312\site-packages\googleapiclient\discovery_cache\__init__.py", line 72, in get_static_doc
    with open(os.path.join(DISCOVERY_DOC_DIR, doc_name), "r") as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument: '<PythonBin>\\local-packages\\Python312\\site-packages\\googleapiclient\\discovery_cache\\documents\\**mybusiness**.v4.json'

I found the information that the mybusisness module is deprectated but still supported, so after all it should work? I also found information that I should use mybusinessbusinessinformation

service = build('**mybusinessbusinessinformation**', 'v1', developerKey=API_KEY)

# Request reviews
reviews = service.accounts().locations().reviews().list(parent=location_id).execute()

# Print review data
for review in reviews.get('reviews', []):
    print(f"Review: {review['review']}")
    print(f"Rating: {review['starRating']}")
    print('---')

Now I get an error that "'Resource' object has no attribute 'reviews'":

Traceback (most recent call last):
  File "<PythonBin>\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<PythonBin>\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "<PythonBin>\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 430, in main        
    run()
  File "<PythonBin>\bundled\libs\debugpy\adapter/../..\debugpy\launcher/../..\debugpy/..\debugpy\server\cli.py", line 284, in run_file    
    runpy.run_path(target, run_name="__main__")
  File "<PythonBin>\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<PythonBin>\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "<PythonBin>\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "C:\src\DownloadGoogleReviewToDWH - test 2.py", line 9, in <module>
    reviews = service.accounts().locations().reviews().list(parent=location_id).execute()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Resource' object has no attribute 'reviews'

I began reading the documentation but I did not find anything on where to find reviews: https://developers.google.com/my-business/reference/businessinformation/rest/v1/accounts.locations

What is a solution to this?

Upvotes: 1

Views: 75

Answers (1)

Andon Totev
Andon Totev

Reputation: 324

Reviews are not migrated to v1 APIs, they still are using v4: https://developers.google.com/my-business/reference/rest/v4/accounts.locations.reviews/list. You have the following problems with your code:

  1. Building the service - correct name is mybusiness (no ** around it) and the version is v4. googleapi client has discovery documents in the library but v4 is not there hence the error when it attempts discovery. To fix this you can either download document from https://developers.google.com/static/my-business/samples/mybusiness_google_rest_v4p9.json and save it locally then use build_from_document or you can point discoveryServiceUrl to the link above
  2. location_id needs to be in v4 format - accounts/xxxx/location/xxxxx

Upvotes: 1

Related Questions