0

While attempting to attest with SafetyNetClient.attest(...) in the latest Play-Services v11.0.1, the client errors out and returns an ApiException.

code snippet:

byte[] nonce = getRequestNonce();
String apiKey = "<api key>";
mSafetyNetClient.attest(nonce, apiKey)
        .addOnCompleteListener(new OnCompleteListener<SafetyNetApi.AttestationResponse>() {
            @Override
            public void onComplete(@NonNull Task<SafetyNetApi.AttestationResponse> task) {
            ...
            }
        });
...
private byte[] getRequestNonce() {
    String nonceData = "Safety Net Sample: " + System.currentTimeMillis();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    byte[] bytes = new byte[24];
    mRandom.nextBytes(bytes);
    try {
        byteStream.write(bytes);
        byteStream.write(nonceData.getBytes());
    } catch (IOException e) {
        return null;
    }

    return byteStream.toByteArray();
}

Logcat output:

06-20 16:43:57.987 18222-18222/com.example.test W/System.err: com.google.android.gms.common.api.ApiException
06-20 16:43:57.987 18222-18222/com.example.test W/System.err:     at com.google.android.gms.common.internal.zzb.zzx(Unknown Source)
06-20 16:43:57.987 18222-18222/com.example.test W/System.err:     at com.google.android.gms.common.internal.zzbi.zzy(Unknown Source)
06-20 16:43:57.987 18222-18222/com.example.test W/System.err:     at com.google.android.gms.common.internal.zzbj.zzo(Unknown Source)
06-20 16:43:57.987 18222-18222/com.example.test W/System.err:     at com.google.android.gms.internal.zzbbd.zzb(Unknown Source)
06-20 16:43:57.987 18222-18222/com.example.test W/System.err:     at com.google.android.gms.internal.zzbbd.setResult(Unknown Source)
06-20 16:43:57.988 18222-18222/com.example.test W/System.err:     at com.google.android.gms.internal.zzcsh.zza(Unknown Source)
06-20 16:43:57.988 18222-18222/com.example.test W/System.err:     at com.google.android.gms.internal.zzcrw.onTransact(Unknown Source)
06-20 16:43:57.988 18222-18222/com.example.test W/System.err:     at android.os.Binder.execTransact(Binder.java:565)

Can anyone please help.

Google api doc says Exception to be returned by a Task when a call to Google Play services has failed.

Can any one please help. Thanks in advance

indraja machani
  • 679
  • 1
  • 9
  • 25

1 Answers1

0

It is very likely that your device doesn't have the latest version of Google Play Services installed.

On one hand you have the API, which as you know, in version 11.0.0 moved from using SafetyNetApi to using SafetyNetClient.

On the other hand you have Google Play Services, which in turn performs the functionality you need, as requested by the API. However, even if you build your app with the latest version of the API, there are no guarantees that the device in which your app is running does indeed have the latest version of Play Services.

Copying verbatim from the documentation:

Before you use the SafetyNet Attestation API, you must ensure that the correct version of Google Play services is installed on the user's device. If an incorrect version is installed, your app might stop responding after calling the API. If your app detects that an incorrect version is installed, you should ask the user to update the Google Play services app on their device.

To check whether the installed version of Google Play services is compatible with the version of the Android SDK that you're using, call the isGooglePlayServicesAvailable() method, as shown in the following code snippet:

if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)
        == ConnectionResult.SUCCESS) {
  // The SafetyNet Attestation API is available.
}
Oscar
  • 346
  • 2
  • 11