I am trying to implement PlayIntegrity API to my Android app, but I don't know how to decrypt and verify the token using Google's servers.
I followed the documentation up to this point:

And now I am stuck on making the decode request to googleapis. I don't understand how does this instruction work.
I created a Service Account and I downloaded JSON credentials file and put it into my Laravel project, then I tried this piece of code:
$client = new Client();
$client->setAuthConfig(storage_path('app/integrity_check_account.json'));
$client->addScope(PlayIntegrity::class);
$httpClient = $client->authorize();
$result = $httpClient->request('POST', 'https://playintegrity.googleapis.com/v1/my.package.name', [
'headers' => ['Content-Type' => 'application/json'],
'body' => "{ 'integrity_token': 'token' }"
]);
dd($result);
So I having two issues with this code:
I have spent hours to get it work with node js. Sometimes Google is very terrible to document/explain and check its own code.
So I post this for anyone who is looking for integrity decryption with a node js server. The only example I could found is directly inside the node playintegrity module of googleapis. Based on this example here my working code:
async function getAppToken() {
const auth = new google.auth.GoogleAuth({
keyFile: 'secret.json',
scopes: ['https://www.googleapis.com/auth/playintegrity'],
});
const authClient = await auth.getClient();
google.options({auth: authClient});
const res = await playintegrity.decodeIntegrityToken (
{
packageName: 'com.example.myapp',
requestBody:
{
"integrityToken": "myToken"
}
}
);
console.log(res.data);
return res.data;
}
You can call that function like this
getAppToken()
.then(data => {
console.log(data);
})
.catch(e => {
console.error(e);
throw e;
});
And here we go! Hum... no, hold on. You also have to fix the integrity api. Go to your node project and find the v1.js file in the playintegrity module
it should be here: \node_modules\googleapis\build\src\apis\playintegrity
Now open it and add this line in the Playintegrity constructor
this.decodeIntegrityToken = this.v1.decodeIntegrityToken;
To get that
class Playintegrity {
constructor(options, google) {
this.context = {
_options: options || {},
google,
};
this.v1 = new Resource$V1(this.context);
this.decodeIntegrityToken = this.v1.decodeIntegrityToken;
}
}
Now it should work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With