2

I am no professional Android developer and I'm having problems to get the Google PlayIntegrity token from my app to decode via PHP through my back-end server and couldn't find much help online.

I included the Google PHP API onto my server, integrated the API into my app and activated&linked it on the Play Console. But it seems I am doing something wrong, I am getting the following PHP error messages:

Warning: Attempt to read property "appLicensingVerdict" on null

My code:

<?php
namespace foo;

use Google\Client;
use Google\Service\PlayIntegrity;
use Google\Service\PlayIntegrity\DecodeIntegrityTokenRequest;

require_once __DIR__ . '/google/vendor/autoload.php';

// Google Integrity token, as obtained from Google through my app:
$token = $_POST['IntegrityToken'];

$client = new Client();
$client->setAuthConfig('google/credentials.json');

$client->addScope(PlayIntegrity::PLAYINTEGRITY);
$service = new PlayIntegrity($client);
$tokenRequest = new DecodeIntegrityTokenRequest();
$tokenRequest->setIntegrityToken($token);
$result = $service->v1->decodeIntegrityToken('com.myapp.game', $tokenRequest);

// Read and handle the JSON response:
        
$appLicensingVerdict = $result->accountDetails->appLicensingVerdict;
// ... more fields are available in the JSON... see Google documentation

// Check/verify the obtained response values.....

?>

Any help would be much appreciated! Many thanks!

lukey2000
  • 41
  • 4
  • Yes, wrong scope/name/namespace, please see the accepted answer for the full classname, watch for the `use` clause, ref: https://www.php.net/manual/en/language.namespaces.importing.php – hakre Dec 18 '22 at 12:56
  • Thank you very much hakre! Unfortunately I can't get the accepted answer to work: HTTP ERROR 500. Can't this be done with the /vendor/autoload.php somehow? That's how it worked to check in-app purchases and I didn't need namespace or use clause in my original code. – lukey2000 Dec 19 '22 at 07:57
  • Sure, it's done with that. But there is a gap to mind: The correct classname is needed. Only if you bring to together a working autoload configuration with the correct classname - then and only then - classes are found. Compare with the PHP manual for both autoloading and the namespace chapter. – hakre Dec 19 '22 at 08:01
  • The linked accepted answer contains such an autoload and classname configuration that is working. Compare to your benefit. You only have a class-not-found error. – hakre Dec 19 '22 at 08:05
  • Will do, thank you very much once again for your super quick replies :-) – lukey2000 Dec 19 '22 at 08:19
  • The class is found now and the request seems to go out without any issues, but I am getting this problem now when I echo the verdict from $result: "Warning: Attempt to read property "appLicensingVerdict" on null" I don't use lavarel so I tried without inlcuding the Illuminate uses and the controller class. Sorry to bother you again, but do you know if there is a chance to read the json response from the google server with some plain PHP code? I am wondering why this is so complicated... – lukey2000 Dec 19 '22 at 13:11
  • Yes, leave the Laravel part out when it is unrelated in your case. And leaving it out is not the cause of the issue. Can you add the current code to your question (first at the end) so that it is visible? This is normally a very small error. – hakre Dec 19 '22 at 13:32
  • I have edited the question with the current code. It would be awesome if it really only was a very small error :-) Many thanks to you again! – lukey2000 Dec 19 '22 at 14:47
  • NP. You normally have to verify `$result->accountDetails` first, before accessing the `appLicensingVerdict`. The error tells that is is NULL, probably undefined. Perhaps do a `var_dump($result)` first and inspect the output. Just a bit of debugging work may already shed you some light. – hakre Dec 19 '22 at 18:16
  • That's great to read. I've reopened the question so that you can leave your resolution as an answer (that's good practice on Stackoverflow). This will not have the resolution burried within comments. Give me a ping here in comments afterwards so I can take a look. Cheers! And welcome again to SO. – hakre Dec 21 '22 at 14:43
  • All done, great thanks again! This platform is awesome, without it I as a non-professional could have never got any of my apps working. – lukey2000 Dec 21 '22 at 20:08
  • Great to read that. After some time you can accept the answer (do that then!) and check the code block formatting I've edited, with a bit of creativity you can highlight things so the answer is easy to grasp. – hakre Dec 21 '22 at 21:14

1 Answers1

2

Also thanks to hakre's help, I was able to solve the problems I was facing. The code works fine with tokenPayLoadExternal included:

$appLicensingVerdict = $result->tokenPayloadExternal # <-- this
                              ->accountDetails
                              ->appLicensingVerdict
    ;
hakre
  • 193,403
  • 52
  • 435
  • 836
lukey2000
  • 41
  • 4