Creating the Service Account
Creating the Service Account
To begin, you will need a Service Account to access the API on behalf of the user.
Select "Create new project" and click on the "Link project" button at the bottom.
Once the project is created, you'll see a new screen with the linked project.
Under the Service accounts section, click on the "Create new service account" button to begin creating the account.
Click on the "Google Cloud Platform" link in the dialog.
Make sure that the project is selected.
Then click on "CREATE SERVICE ACCOUNT."
Give the account a name and continue to the next step.
For the role, select "Service Account User," then click on "Done" to complete the process.
Go back to the API Access page and click "Done" to close the dialog.
The page should refresh and your account will appear in the Service accounts section.
Granting Permissions
The next thing we need to do is to give access to the account to perform actions with the Google Play Developer API.
If you want to allow all access, check the Admin permission. For Google Play Billing, check the financial data and Manager orders option.
Then click on "Invite user" to complete the process.
Retrieving the Service Account Information
After you grant the necessary permissions, go back to the API access page and click on the "Google Cloud" button next the account.
Click on the account.
Select the "Keys" tab.
Click on "ADD KEY" and then "Create new key."
Select JSON and then click on "CREATE" to create the file.
A file should be downloaded automatically. It contains information about the account that we'll need to generate the JSON Web Token.
Sample JSON Key file
Generating the JSON Web Token
After you obtain the private key for the service account, you’ll need to create a JSON Web Token to request an access token to communicate with the Google Play Developer API.
Each part is encoded using the Base64url encoding format and separated by a period.
{ Base64Url Header } . { Base64Url Claim Set } . { Base64Url Signature }
The header and claim set are JSON objects that contains information about the token.
To begin, create a new JavaScript file call jwt.js. Then create a JSON object call header.
Now for the claim set, create another JSON object call claimSet.
const now = new Date().getTime() / 1000;
“aud” represents the url we are using to get the token from. It is always going to https://oauth2.googleapis.com/token.
Encoding the header and claim set
Generating the signature
Then grab the signer, call sign, pass in the private key for the first parameter, use “base64” for the second, and store the signature.
const signature = signer.sign(privateKey, "base64");
const encodedSignature = signature.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
Obtaining the JWT
const jwt = `${encodedHeader}.${encodedClaimSet}.${encodedSignature}`;
If we print it out, we’ll get some long-encoded string which is what we want.
eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJiaWxsaW5nc2VydmljZWFjY291bnRAYXBpLTQ2NDE1NTM5ODcyMzczOTQyMDUtNTUxMzU5LmlhbS5nc2VydmljZWFjY291bnQuY29tIiwiaWF0IjoxNjQyNDc0Nzg3LjAyOCwiZXhwIjoxNjQyNDc4Mzg3LjAyOCwic2NvcGUiOiJodHRwczovL3d3dy5nb29nbGVhcGlzLmNvbS9hdXRoL2FuZHJvaWRwdWJsaXNoZXIiLCJhdWQiOiJodHRwczovL29hdXRoMi5nb29nbGVhcGlzLmNvbS90b2tlbiJ9.vagBHLgdfzyaDO0mutpVZWqzXcrK4K0M7o6s6jQNFB5m50mcvJyLTI9E6RcoHv14ujGodP-NvKSdsg4-mASBV6Ay65bjsQta4GNrpVRAp8Xse8Rsl3Z8j7EtDOtFvdEs9TjHvj_LPThtr0FlFE3yr-513cJmyeAd53kxDOdKeHguuTOrvtp58IrYoAKyuJURY20ijg7ibZD07r-lcDK2cx9yejHs-TYGcvEjAxL7PECEP4JbXaA2NmTKoiNClXfm533Qq-8JqOHUPINx6iKtIkkLmZmzHDvArWZ5WyuvnJuQz83Eyk-iwgY7H_4_6zuv1IU-VG69vaitipCPfMh53g
OAuth and Access Token
const https = require("https");
Full function
Testing
To test this, create an async function call test() and call the getOAuthToken() function inside.
Call the test() function and run the app.
You should see an response with the access token.