You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google PlayIntegrity API PHP验证Token报错求助:读取null属性问题

解决Google Play Integrity Token解码时"Attempt to read property 'appLicensingVerdict' on null"的问题

这个警告的核心原因是$result->accountDetails为null,导致你无法访问它的appLicensingVerdict属性。下面是具体的排查步骤和修复方案:

1. 先捕获API调用的错误信息

Google Play Integrity API在调用失败时会返回具体错误,但你的代码没有处理异常,直接尝试读取属性。先添加异常捕获,定位具体问题:

<?php
namespace foo;

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

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

// 先判断POST参数是否存在,避免Undefined index错误
if (!isset($_POST['IntegrityToken'])) {
    exit("缺少IntegrityToken参数");
}
$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);

try {
    $result = $service->v1->decodeIntegrityToken('com.myapp.game', $tokenRequest);
} catch (Exception $e) {
    // 记录错误日志,方便排查
    error_log("Play Integrity API调用失败:" . $e->getMessage());
    exit("Token解码失败:" . $e->getMessage());
}

// 处理响应
if ($result->accountDetails) {
    $appLicensingVerdict = $result->accountDetails->appLicensingVerdict;
    // 继续处理其他字段
} else {
    error_log("无法获取账号详情,accountDetails为空");
    // 根据业务逻辑处理,比如返回默认状态或提示信息
}
?>

2. 排查服务账号与权限配置

  • 确认credentials.json对应的服务账号已在Play Console中授权:
    1. 打开Play Console,进入「设置 > API访问」
    2. 确保该服务账号已被添加为「授权的测试用户」或拥有「Play Integrity API」的访问权限
  • 检查包名com.myapp.game是否与App的实际包名完全一致(包括大小写,虽然Android包名通常是小写,但必须严格匹配)

3. 验证Token的有效性

  • 确认App中获取Token的逻辑正确:调用PlayIntegrityManager.requestIntegrityToken()时,传递的nonce、packageName等参数正确,且Token在传递到后端时没有被篡改、截断或编码错误(建议用POST方式传递,避免GET的长度限制)
  • 检查Token是否过期:Play Integrity Token的有效期很短(通常几分钟),确保传递到后端时仍在有效期内

4. 处理accountDetails为空的场景

即使API调用成功,accountDetails也可能为null(比如用户未关联Google账号、设备环境不支持等),所以必须先判断是否存在,再访问其属性,避免警告或错误。

内容的提问来源于stack exchange,提问作者lukey2000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 13:25:16