0

我们正在尝试将安全网 API 添加到我们的应用程序中。当我们在真实设备上测试时,一切正常,但是在模拟器上测试时,安全网服务器没有响应。API 的目的是检测模拟设备,所以不知道为什么它不能在模拟器上工作。

以下是 API 调用:

SafetyNet.getClient(activity).attest(getRequestNonce(nonceData), "<Key-here>").addOnSuccessListener(activity, new OnSuccessListener() {
    public void onSuccess(AttestationResponse response) {
        String[] jwtParts = response.getJwsResult().split("\\.");
        if(jwtParts.length == 3) {
            String sharedpreferences = new String(Base64.decode(jwtParts[1], 0));
            SharedPreferences editor = context.getSharedPreferences("DecodedPayload", 0);
            Editor editor1 = editor.edit();
            editor1.putString("decodedPayload", sharedpreferences);
            editor1.commit();
            Log.d("ContentValues", "The Safety net response is: " + sharedpreferences);
        } else {
            SharedPreferences sharedpreferences1 = context.getSharedPreferences("DecodedPayload", 0);
            Editor editor2 = sharedpreferences1.edit();
            editor2.putString("decodedPayload", "CND");
            editor2.commit();
            Log.d("ContentValues", "The safety net response could not be decoded");
        }

    }
}).addOnFailureListener(activity, new OnFailureListener() {
    public void onFailure(@NonNull Exception e) {
        if(e instanceof ApiException) {
            ApiException apiException = (ApiException)e;
            Log.d("ContentValues", "Error while fetching safety net result: " + ((ApiException)e).getStatusCode() + " " + ((ApiException)e).getStatusMessage());
            SharedPreferences sharedpreferences = context.getSharedPreferences("DecodedPayload", 0);
            Editor editor = sharedpreferences.edit();
            editor.putString("decodedPayload", "ERR");
            editor.commit();
            Log.d("ContentValues", "The safety net response could not be decoded");
        } else {
            Log.d("ContentValues", "Unknown Error while fetching safety net results: " + e.getMessage());
        }

    }
});

}

即使等待了 30 秒,也没有一个处理程序得到响应。有人可以帮忙吗。

4

2 回答 2

1

您运行的模拟器很可能没有安装 Google Play 服务。由于 Google Play Services 负责实现 Attestation,因此 Attestation 根本无法在没有 Google Play Services 的设备上运行。

使用 Attestation 或任何其他 Google Play Services API 时,您应该检查 Google Play Services 是否已安装,并且是您需要的版本(最好是检查它是否为最新版本)

从历史上看,官方 AVD 图像不包括 Google Play 服务,从技术上讲,您可以旁加载它。然而,在 2017 年 4 月左右,现在有包含 Play 商店(以及随之而来的 Google Play 服务)的官方 AVD 图像。

于 2018-06-05T02:52:48.610 回答
0

这是一个迟到的回应,但也许将来有人需要。尝试使用 SafetyNet 附带的不同侦听器,尽管 SafetyNet 返回成功,但所有完整和成功的侦听器都不会响应。您还可以在 Android 设备验证 API 仪表板中检查来自 Google Cloud Platform 的 API 调用和成功、失败情况。

        SafetyNet.getClient(this).attest(generateOneTimeRequestNonce(), "API_KEY")
                .addOnFailureListener { e ->
                    Log.i(TAG, "SafetyNet callback fail")
                }
                .addOnSuccessListener { resp ->
                    Log.i(TAG, "SafetyNet callback success")
                    val response = parseJsonWebSignature(resp.jwsResult)!!

                    when {
                        response.isCtsProfileMatch -> {
                            //profile of the device running your app matches the profile of a device that has passed Android compatibility testing.

                        }
                        response.isBasicIntegrity -> {
                            //then the device running your app likely wasn't tampered with, but the device has not necessarily passed Android compatibility testing.

                        }
                        else -> {
                            //handle fail, maybe warn user device is unsupported or in compromised state? (this is up to you!)

                        }
                    }
                }
于 2018-10-25T12:55:07.110 回答