3

For saftey environment check in android I was using

https://developer.android.com/training/safetynet/attestation.html

SafetyNet.getClient(this).attest(nonce, API_KEY)
   .addOnSuccessListener(this,successListner)
   .addOnFailureListener(this,failureListener)

for which I am getting failure callback with network error null. Can anybody please throw some light on this ?

but https://medium.com/@hargoyal/secure-android-app-with-safetynet-8e367a1c8ad0

SafetyNet.SafetyNetApi.attest(mGoogleApiClient, nonce)
                .setResultCallback(new ResultCallback<SafetyNetApi.AttestationResult>() {
                    @Override
                    public void onResult(SafetyNetApi.AttestationResult result) {
                        Status status = result.getStatus();
                        if (status.isSuccess()) {
                            decodeJws(result.getJwsResult());
                            // Indicates communication with the service was successful.
                            // Use result.getJwsResult() to get the result data.
                        } else {
                            // An error occurred while communicating with the service.
                        }
                    }
                });

this approach is working but SafetyNet.SafetyNetApi is deprecated.

Bobstring
  • 460
  • 4
  • 11
Syamkrishnan Ck
  • 193
  • 1
  • 10
  • 1
    What I found is that the SafetyNet API only works when your API key has no Key restrictions, e.g. do NOT select Android Apps and enter your package name with SHA1. Your network error null is because you need to call apiException.getMessage() and NOT apiException.getStatusMessage(). – Hector Nov 28 '17 at 12:30

1 Answers1

2

Recheck the API key or create a new one like here. I was having the same issue and the problem was that I was restricting the key to the wrong package name.

  • Go to the Library page in the Google APIs Console.
  • Search for the Android Device Verification API. When you've found the API, click on it. The Android Device Verification API dashboard screen appears.
  • If the API isn't already enabled, click Enable.
  • If the Create credentials button appears, click on it to generate an API key. Otherwise, click the All API credentials drop-down list and select the API key that is associated with the project for which the Android Device Verification API is enabled.
  • In the sidebar on the left, click Credentials. Copy the API key that appears.
Bobstring
  • 460
  • 4
  • 11