Search code examples
google-apigoogle-api-clientgoogle-api-python-clientservice-accountsgoogle-play-developer-api

Getting authorization token-key for my google service account


I actually want to reply my google play app's comments using this api: https://developers.google.com/android-publisher/reply-to-reviews#gaining_access But in here, it wants me to enter auth_token. First i added service account to my google play. After that, i created a key and i downloaded the json file for my key. Using this json file i tried to get auth. token but output of this code tells me that credentials are invalid and token is "none". I wanna solve this problem. Thanks.

from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = ['https://www.googleapis.com/auth/cloud-platform',
          'https://oauth2.googleapis.com/token',
          'https://accounts.google.com/o/oauth2/auth',
          'https://www.googleapis.com/oauth2/v1/certs',
          'https://www.googleapis.com/robot/v1/metadata/x509/myusername.gserviceaccount.com']
SERVICE_ACCOUNT_FILE = 'service.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

print(credentials._token_uri)
print(dir(credentials))
print(credentials.valid)
print(credentials.token)

Resources that i researched:

https://developers.google.com/identity/protocols/oauth2#serviceaccount https://developers.google.com/identity/protocols/oauth2/service-account#python_1 https://developers.google.com/android-publisher/reply-to-reviews#gaining_access


Note: Also people are sending their API requests like this:

service = build('drive', 'v3', credentials=credentials)

For example above code is for sending api request for google drive. How can i api for my purpose? For example i wanna access google play api using "build".


Solution

  • The key to the build method is that first it takes the API then it takes the version of the api you want to access followed by your credentials.

    service = build('drive', 'v3', credentials=credentials)
    

    If you check Discovery services you will find a list of all google apis

    The entry for android publisher is as follows

     {
          "kind": "discovery#directoryItem",
          "id": "androidpublisher:v3",
          "name": "androidpublisher",
          "version": "v3",
          "title": "Google Play Android Developer API",
          "description": "Lets Android application developers access their Google Play accounts.",
          "discoveryRestUrl": "https://androidpublisher.googleapis.com/$discovery/rest?version=v3",
          "icons": {
            "x16": "https://www.gstatic.com/images/branding/product/1x/googleg_16dp.png",
            "x32": "https://www.gstatic.com/images/branding/product/1x/googleg_32dp.png"
          },
          "documentationLink": "https://developers.google.com/android-publisher",
          "preferred": true
        },
    

    Which means that the following should be what you are looking for.

    service = build('androidpublisher', 'v3', credentials=credentials)