2

I'm calling SafetyNet Api using Google Client but it not responding the correct response.

   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.
                    }
                }
            });

I'm getting below error message in result method.

Status{statusCode=NETWORK_ERROR, resolution=null}

Any kind of help would be highly appreciated.

Robin Royal
  • 1,788
  • 1
  • 18
  • 29

2 Answers2

0

This doesn't work because you are using SafetyNetApi, which is no longer supported.

Starting with Google Play Services 11.0.0, you should now get an API key, and use SafetyNetClient instead.

You may also want to take a look at 10 things you might be doing wrong when using the SafetyNet Attestation API.

Oscar
  • 346
  • 2
  • 11
0

First you have to generate nonce by following method

   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();
}

Afterwords use safety net client attestation 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.
                }
            });
        }

Reference: Sample Code Offline verification

Sample Code Online verification using google api

Dishant Walia
  • 701
  • 5
  • 8