我正在使用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,分辨率=空}
任何帮助将不胜感激。
答案 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.
}
});
}