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

Volley调用Google Play Integrity服务器解密验证无响应问题求助

Play Integrity服务端解密验证代码问题排查

你的代码存在几个关键问题,导致请求既无响应也无报错,逐一拆解:

1. 变量作用域错误,请求从未执行

jsonObjectRequest是定义在onComplete成功分支内的局部变量,外部的MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)根本访问不到这个对象——等于请求从未被提交到Volley队列,自然不会有任何反馈。

2. 网络请求配置完全错误

  • 请求方式错误:Decode Integrity Token的API要求用POST请求,你用了GET,而GET请求无法携带JSON请求体。
  • URL格式错误:你把curl命令的参数直接拼进了URL,正确的URL应该是https://playintegrity.googleapis.com/v1/你的应用包名:decodeIntegrityToken,不需要附加-d和JSON字符串。

3. 缺少认证信息

调用Google的Play Integrity解密API必须携带认证信息(API密钥或OAuth2令牌),否则请求会被401权限拒绝。

4. 空回调无反馈

onResponse和onErrorResponse里没有任何日志输出,就算请求有结果或错误,你也看不到任何信息。

5. Nonce处理可能不规范

Play Integrity要求Nonce是Base64编码的随机字符串,确保每次请求唯一以防止重放攻击,你需要确认mynonce是否符合要求。


修正后的代码示例

// 初始化Integrity Manager
IntegrityManager integrityManager = IntegrityManagerFactory.create(getApplicationContext());

// 生成符合要求的Nonce(示例:随机字符串Base64编码)
String rawNonce = UUID.randomUUID().toString();
String encodedNonce = Base64.encodeToString(rawNonce.getBytes(StandardCharsets.UTF_8), Base64.NO_WRAP);

// 请求Integrity Token
Task<IntegrityTokenResponse> integrityTokenResponse = integrityManager.requestIntegrityToken(
        IntegrityTokenRequest.builder()
                .setNonce(encodedNonce)
                .build()
);

integrityTokenResponse.addOnCompleteListener(taskplay -> {
    if (taskplay.isSuccessful()) {
        String myToken = taskplay.getResult().token();
        String packageName = getPackageName();
        String url = "https://playintegrity.googleapis.com/v1/" + packageName + ":decodeIntegrityToken";

        // 构造请求体
        JSONObject requestBody = new JSONObject();
        try {
            requestBody.put("integrity_token", myToken);
        } catch (JSONException e) {
            e.printStackTrace();
            return;
        }

        // 构造POST请求,携带API密钥(替换成你的实际API密钥)
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST,
                url,
                requestBody,
                response -> {
                    // 处理成功响应,打印日志
                    Log.d("PlayIntegrity", "解密响应:" + response.toString());
                },
                error -> {
                    // 处理错误,打印日志
                    Log.e("PlayIntegrity", "请求错误:", error);
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                // 添加API密钥认证,替换成你的API密钥
                headers.put("Authorization", "Bearer YOUR_GOOGLE_CLOUD_API_KEY");
                headers.put("Content-Type", "application/json");
                return headers;
            }
        };

        // 将请求加入Volley队列(注意这里要在回调内部执行)
        MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
    } else {
        // 打印Token请求失败的错误
        Log.e("PlayIntegrity", "Token请求失败:", taskplay.getException());
    }
});

额外注意事项

  • API密钥获取:需要在Google Cloud控制台创建API密钥,启用Play Integrity API,并限制密钥的使用范围避免泄露。
  • 安全建议:不要在客户端直接调用解密API,这会泄露你的API密钥,正确做法是在自己的后端服务中调用Google的解密API,客户端只把Integrity Token发送到你的后端。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 05:55:15