pythonandroidautomationgoogle-play-servicesgoogle-reviews

How do I access Google Playstore API to get reviews of our app using Python script?


I have a service account and private key generated for the account with the app.

testxyz.json - contains the private key and service account information.

Here's the script:

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
import ssl
import json

ssl._create_default_https_context = ssl._create_unverified_context


credentials = ServiceAccountCredentials.from_json_keyfile_name('testxyz.json',scopes=['https://www.googleapis.com/auth/androidpublisher'])


http = httplib2.Http()
http = credentials.authorize(http)
service = build('androidpublisher', 'v3', http=http)
print(service)

package_name = "xtestx"
reviews_resource = service.reviews()
print(reviews_resource)
reviews_page = reviews_resource.list(packageName=package_name,maxResults=100).execute()
reviews_list = reviews_page["reviews"]

infinite_loop_canary = 100
while "tokenPagination" in reviews_page:
    reviews_page = reviews_resource.list(packageName=package_name,maxResults=100).execute()
    token=reviews_page["tokenPagination"]["nextPageToken"],
    maxResults=100).execute()
    reviews_list.extend(reviews_page["reviews"])
    infinite_loop_canary -= 1
    if infinite_loop_canary < 0:
        break

The line in the script - reviews_resource.list(packageName=package_name,maxResults=100).execute()- is throwing "The caller does not have permission"

I am not exactly sure what needs to be done. Any help is appreciated.


Solution

  • I am posting the answer here since it took quite some time for me to figure this out, maybe it'll help someone else.

    What you need before:

    1. Service account from https://console.developers.google.com

    2. A private key for the SA. (used .p12 file)

    3. In Play Console, we need to grant access to this SA from Developer account -> API access.

    4. Once we click on Grant Access(from step 3), we have Permissions dialog - enable "Reply to reviews" for the required apps.

       from apiclient.discovery import build
       import httplib2
       from oauth2client import client
       from oauth2client.service_account import ServiceAccountCredentials
      
       SERVICE_ACCOUNT_EMAIL = ('associated_service_email_address_goes_here')
      
       #this is the private key file
       key = 'xxx.p12' 
       scope='https://www.googleapis.com/auth/androidpublisher'
      
       credentials= ServiceAccountCredentials.from_p12_keyfile(SERVICE_ACCOUNT_EMAIL,
       key,
       scopes=[scope])
      
       package_name = "xtestx"
      
       try:
           http = httplib2.Http()
           http = credentials.authorize(http)
           service = build('androidpublisher', 'v3', http=http)
      
           reviews_resource = service.reviews()
           print(reviews_resource)
           reviews_page = reviews_resource.list(packageName=package_name,maxResults=100).execute()
           reviews_list = reviews_page["reviews"]
       except Exception as e:
           ...
      

    The permissions created the issue from me.