Create a Service Account to generate Google API Access Tokens for Validating In-App Purchases on Android

Tiempo de lectura: 2 minutos

Today we’re going to learn how to generate a Service Account account that will serve us for verifying purchases or generating an authentication token in Google.

Birds flying - pexels

The first thing we’ll do is go to https://console.cloud.google.com/iam-admin/serviceaccounts/create

We create an account:

We choose the Role and put EDITOR (very important).

We press once created, click on Adminstrator keys:

We click now on create new key

You choose JSON.

We copy that JSON and bring it to our back.

To obtain the access token, we will use this function

# Get access token (duration 1 hour) # Service Account JSON path SERVICE_ACCOUNT_FILE = "service-account.json" # Google Play Developer API scope SCOPES = ["https://www.googleapis.com/auth/androidpublisher"] def get_access_token(): # Load credentials from JSON credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES ) # Refresh/obtain token credentials.refresh(Request()) return credentials.token

We will use this to call it:

 token = get_access_token() print("Access Token:", token)

We now need to go to the Google Play publish console.

We will go after that.

Promote the user who we created:

We assign the app to which it will affect:

You will be granted permission to view financial information and access subscriptions or purchases.

We have to update the purchase or subscription in-app if they were created before creating this user.

Leave a Comment