Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode integrity token using Google PlayIntegrity API

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:

Making request to the API

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:

  1. Am I adding the scope correctly?
  2. Am I making the request correctly? Because it is not working as I am getting 404 error.
like image 423
hiddeneyes02 Avatar asked May 10 '26 06:05

hiddeneyes02


1 Answers

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

like image 115
Steeve Favre Avatar answered May 11 '26 19:05

Steeve Favre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!