Android:SafetyNet证明设备验证响应网络错误

时间:2018-12-24 11:27:51

标签: android safetynet

我正在使用Google客户端致电SafetyNet Api,但未响应正确的响应。

   SafetyNet.SafetyNetApi.attest(mGoogleApiClient, generateNonce())
            .setResultCallback(new ResultCallback<SafetyNetApi.AttestationResult>() {
                @Override
                public void onResult(SafetyNetApi.AttestationResult result) {
                    Status status = result.getStatus();
                    String data = decodeJws(result.getJwsResult());

                    if (status.isSuccess()) {
                        // Indicates communication with the service was successful.
                        // Use result.getJwsResult() to get the result data.
                    } else {
                        // An error occurred while communicating with the service.
                    }
                }
            });

我在结果方法中得到以下错误消息。

状态{statusCode = NETWORK_ERROR,分辨率=空}

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

这不起作用,因为您使用的是SafetyNetApi,不再受支持。

从Google Play服务11.0.0开始,您现在应该get an API key,而改用SafetyNetClient

您可能还想看看10 things you might be doing wrong when using the SafetyNet Attestation API

答案 1 :(得分:0)

首先,您必须通过以下方法生成随机数

   private static byte[] getRequestNonce() {
    String data = String.valueOf(System.currentTimeMillis());
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    byte[] bytes = new byte[24];
    Random random = new Random();
    random.nextBytes(bytes);
    try {
        byteStream.write(bytes);
        byteStream.write(data.getBytes());
    }catch (IOException e) {
        return null;
    }
    return byteStream.toByteArray();
}

后记使用安全网客户端证明api

 SafetyNet.getClient(context).attest(nonce, <API KEY>).addOnSuccessListener(new OnSuccessListener<SafetyNetApi.AttestationResponse>() {
                @Override
                public void onSuccess(SafetyNetApi.AttestationResponse attestationResponse) {
                    // parse response 

                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    // An error occurred while communicating with the service.
                }
            });
        }

参考:Sample Code Offline verification

Sample Code Online verification using google api