我想使用SafetyNet Attestation API(注意,由于不赞成使用其使用的方法,因此本文档似乎已过时)。使用最新版本的Play服务(11.0.1),我想到了以下代码:
SecureRandom secureRandom = new SecureRandom();
byte[] nonce = new byte[16];
secureRandom.nextBytes(nonce); // just some random bytes for testing
SafetyNet.getClient(this)
.attest(nonce, API_KEY)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
SafetyNetApi.AttestationResponse result = task.getResult();
String jws = result.getJwsResult();
Log.d(TAG, "JWS: " + jws);
} else {
Exception e = task.getException();
if (e instanceof ApiException) {
Log.e(TAG, "Attestation failure: " + ((ApiException) e).getStatusMessage() + ", code: " + ((ApiException) e).getStatusCode(), e);
} else {
Log.e(TAG, "Attestation failure: " + e, e);
}
}
});
其中
API_KEY
是Google Developer Console中的API密钥。此代码在Activity
的onCreate(...)
中调用。无论我尝试什么,都会导致失败,并且e
是ApiException
的实例,但是由于状态消息为null
并且状态码为8,因此它无法提供有关出了什么问题的任何有用信息-根据the documentation-是“内部错误”。我试图用5秒的延迟来称呼它,但没有成功。测试设备具有API 24和Google Play服务11.0.55。任何人都知道哪里出了问题,这有什么解决方案?
编辑:旧的
SafetyNet.SafetyNetApi.attest(googleApiClient, nonce)
方法似乎工作正常,但已弃用,所以我不想使用它。 最佳答案
根据此thread,如果您收到错误代码8(INTERNAL_ERROR
),请在开发控制台中再次检查您的应用注册。请注意,每个已注册的Android客户端均由(包名称,Android签名证书SHA-1)对唯一标识。如果您在调试和生产环境中具有多个程序包名称/签名证书,请确保对其进行注册。
核实:
打开Credentials page并选择您的项目
确保每对都有一个Android输入的OAuth 2.0客户端ID。要为您的Android客户端创建新的OAuth 2.0客户端ID,请从下拉菜单中选择新建凭据-> OAuth2客户端ID,选择Android,然后在其中输入包名称/签名证书指纹。
如果不起作用,建议您与Google Play小组联系以寻求帮助。您可以从以下链接访问它们:https://support.google.com/googleplay#topic=3364260&contact=1。