play-integrity-checker-app: Why PlayIntegrity api call resulting in Error code :GOOGLE_SERVER_UNAVAILABLE Where am i going wrong ?

Following your example but unfortunately am not getting the token since it is giving an error as GOOGLE_SERVER__UNAVAILABLE. Have done the ground works like Play & Cloud console enablement.

You can check my code:

      @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       // playIntegritySetup.lol();
       getToken();
   }

   private void getToken() {
       String nonce = Base64.encodeToString(generateNonce(50).getBytes(),   Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING);

       // Create an instance of a manager.
       IntegrityManager integrityManager = IntegrityManagerFactory.create(getApplicationContext());

       // Request the integrity token by providing a nonce.
       Task<IntegrityTokenResponse> integrityTokenResponse = integrityManager.requestIntegrityToken(
               IntegrityTokenRequest.builder()
                       .setNonce(nonce)
                       .build());

       integrityTokenResponse.addOnSuccessListener(new OnSuccessListener<IntegrityTokenResponse>() {
           @Override
           public void onSuccess(IntegrityTokenResponse integrityTokenResponse) {
               String integrityToken = integrityTokenResponse.token();
               SplashActivity.this.doIntegrityCheck(integrityToken);
               Log.e("Integrity Token", "integrity token from the app" + integrityToken);

           }
       });

       integrityTokenResponse.addOnFailureListener(e -> showErrorDialog("Error getting token from Google. Google said: " + getErrorText(e)));
   }

   private void doIntegrityCheck(String token) {
       AtomicBoolean hasError = new AtomicBoolean(false);


       Observable.fromCallable(() -> {

                   OkHttpClient okHttpClient = new OkHttpClient();
                   Response response = okHttpClient.newCall(new Request.Builder().url("money control url" + "token from backend server" + token).build()).execute();
                   Log.e("Token", "token from the app" + token);

                   if (!response.isSuccessful()) {
                       hasError.set(true);
                       return "Api request error. Code: " + response.code();

                   }
                   ResponseBody responseBody = response.body();
                   if (responseBody == null) {
                       hasError.set(true);

                       return "Api request error. Empty response";

                   }
                   JSONObject responseJson = new JSONObject(responseBody.string());
                   if (responseJson.has("error")) {
                       hasError.set(true);

                       return "Api request error: " + responseJson.getString("error");

                   }
                   if (!responseJson.has("deviceIntegrity")) {
                       hasError.set(true);

                   }

                   return responseJson.getJSONObject("deviceIntegrity").toString();
               }) // Execute in IO thread, i.e. background thread.
               .subscribeOn(Schedulers.io())
               // report or post the result to main thread.
               .observeOn(AndroidSchedulers.mainThread())
               // execute this RxJava
               .subscribe(new Observer<String>() {
                   @Override
                   public void onSubscribe(Disposable d) {

                   }

                   @Override
                   public void onNext(String result) {
                       if (hasError.get()) {
                           if (result.contains("MEETS_DEVICE_INTEGRITY") && result.contains("MEETS_BASIC_INTEGRITY")) {
                              //Here goes my other code

                           }
                       }
                   }

                   @Override
                   public void onError(Throwable e) {

                   }

                   @Override
                   public void onComplete() {

                   }
               });
   }




 private String getErrorText(Exception e) {
       String msg = e.getMessage();
       if (msg == null) {
           return "Unknown Error";
       }

       //the error code
       int errorCode = Integer.parseInt(msg.replaceAll("\n", "").replaceAll(":(.*)", ""));
       switch (errorCode) {
           case IntegrityErrorCode.API_NOT_AVAILABLE:
               return "API_NOT_AVAILABLE";
           case IntegrityErrorCode.NO_ERROR:
               return "NO_ERROR";
           case IntegrityErrorCode.INTERNAL_ERROR:
               return "INTERNAL_ERROR";
           case IntegrityErrorCode.NETWORK_ERROR:
               return "NETWORK_ERROR";
           case IntegrityErrorCode.PLAY_STORE_NOT_FOUND:
               return "PLAY_STORE_NOT_FOUND";
           case IntegrityErrorCode.PLAY_STORE_ACCOUNT_NOT_FOUND:
               return "PLAY_STORE_ACCOUNT_NOT_FOUND";
           case IntegrityErrorCode.APP_NOT_INSTALLED:
               return "APP_NOT_INSTALLED";
           case IntegrityErrorCode.PLAY_SERVICES_NOT_FOUND:
               return "PLAY_SERVICES_NOT_FOUND";
           case IntegrityErrorCode.APP_UID_MISMATCH:
               return "APP_UID_MISMATCH";
           case IntegrityErrorCode.TOO_MANY_REQUESTS:
               return "TOO_MANY_REQUESTS";
           case IntegrityErrorCode.CANNOT_BIND_TO_SERVICE:
               return "CANNOT_BIND_TO_SERVICE";
           case IntegrityErrorCode.NONCE_TOO_SHORT:
               return "NONCE_TOO_SHORT";
           case IntegrityErrorCode.NONCE_TOO_LONG:
               return "NONCE_TOO_LONG";
           case IntegrityErrorCode.GOOGLE_SERVER_UNAVAILABLE:
               return "GOOGLE_SERVER_UNAVAILABLE";
           case IntegrityErrorCode.NONCE_IS_NOT_BASE64:
               return "NONCE_IS_NOT_BASE64";
           default:
               return "Unknown Error";
       }
   }

   private String generateNonce(int length) {
       String nonce = "";
       String allowed = getNonce();
       for (int i = 0; i < length; i++) {
           nonce = nonce.concat(String.valueOf(allowed.charAt((int) Math.floor(Math.random() * allowed.length()))));
       }
       return nonce;
   }

   public native String getNonce();


   static {
       System.loadLibrary("all-keys");
   }

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

The json should be in the server not the app

Okay I think I remembered. Is your app on Google Play right now?