Example Using Safetynet Recaptcha Api Inwards Android App

Posted on

reCAPTCHA Android API announced every bit purpose of Google Play Services. It’s a elementary event to utilisation SafetyNet reCAPTCHA API inwards Android App. Basically, it follow the steps inwards the tutorial SafetyNet reCAPTCHA API.

– Create a Android Project inwards Android Studio every bit as usually, amongst minSdkVersion of xiv or higher. We demand the Package Names inwards the adjacent step.

– Visit reCAPTCHA Android signup site, fill upwardly inwards the blanks to larn your Site telephone substitution in addition to Secret key. (Get your own, I volition delete my keys subsequently the samples finished.) Copy the keys to your programme every bit String.

– Open SDK Manager inwards Android Studio, brand certain you lot include Google Repository inwards your SDK Tools.

– Add the follow Google Play services APIs into your Gradle dependencies:

    compile 'com.google.android.gms:play-services-base:11.0.0'     compile 'com.google.android.gms:play-services-basement:11.0.0'     compile 'com.google.android.gms:play-services-safetynet:11.0.0'     compile 'com.google.android.gms:play-services-tasks:11.0.0' 

This video exhibit how to:


Modify the code:

MainActivity.java

package com.blogspot.android_er.recaptcha;  import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;  import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.ResultCallback; import com.google.android.gms.common.api.Status; import com.google.android.gms.safetynet.SafetyNet; import com.google.android.gms.safetynet.SafetyNetApi;  populace cast MainActivity extends AppCompatActivity         implements GoogleApiClient.ConnectionCallbacks,         GoogleApiClient.OnConnectionFailedListener{      concluding String SiteKey = "6LdMKyUUAAAAAN0ndw7byI03_qpbpjxKY-mTQnLw";     concluding String SecretKey  = "6LdMKyUUAAAAALjcWovpXgcoXiI4i9ykn1U9qs8I";     soul GoogleApiClient mGoogleApiClient;      Button btnRequest;     TextView tvResult;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          tvResult = (TextView)findViewById(R.id.result);         btnRequest = (Button)findViewById(R.id.request);         btnRequest.setOnClickListener(RqsOnClickListener);          mGoogleApiClient = novel GoogleApiClient.Builder(this)                 .addApi(SafetyNet.API)                 .addConnectionCallbacks(MainActivity.this)                 .addOnConnectionFailedListener(MainActivity.this)                 .build();          mGoogleApiClient.connect();     }      View.OnClickListener RqsOnClickListener = novel View.OnClickListener() {         @Override         populace void onClick(View view) {             tvResult.setText("");              SafetyNet.SafetyNetApi.verifyWithRecaptcha(mGoogleApiClient, SiteKey)                     .setResultCallback(new ResultCallback<SafetyNetApi.RecaptchaTokenResult>() {                         @Override                         populace void onResult(SafetyNetApi.RecaptchaTokenResult result) {                             Status condition = result.getStatus();                              if ((status != null) && status.isSuccess()) {                                  tvResult.setText("isSuccess()\n");                                 // Indicates communication amongst reCAPTCHA service was                                 // successful. Use result.getTokenResult() to larn the                                 // user reply token if the user has completed                                 // the CAPTCHA.                                  if (!result.getTokenResult().isEmpty()) {                                     tvResult.append("!result.getTokenResult().isEmpty()");                                     // User reply token must hold out validated using the                                     // reCAPTCHA site verify API.                                 }else{                                     tvResult.append("result.getTokenResult().isEmpty()");                                 }                             } else {                                  Log.e("MY_APP_TAG", "Error occurred " +                                         "when communicating amongst the reCAPTCHA service.");                                  tvResult.setText("Error occurred " +                                         "when communicating amongst the reCAPTCHA service.");                                  // Use status.getStatusCode() to create upwardly one's heed the exact                                 // fault code. Use this code inwards conjunction amongst the                                 // data inwards the "Handling communication errors"                                 // department of this document to accept appropriate activity                                 // inwards your app.                             }                         }                     });          }     };      @Override     populace void onConnected(@Nullable Bundle bundle) {         Toast.makeText(this, "onConnected()", Toast.LENGTH_LONG).show();     }      @Override     populace void onConnectionSuspended(int i) {         Toast.makeText(this,                 "onConnectionSuspended: " + i,                 Toast.LENGTH_LONG).show();     }      @Override     populace void onConnectionFailed(@NonNull ConnectionResult connectionResult) {         Toast.makeText(this,                 "onConnectionFailed():\n" + connectionResult.getErrorMessage(),                 Toast.LENGTH_LONG).show();     } }  

layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:layout_margin="20dp"     tools:context="com.blogspot.android_er.recaptcha.MainActivity" >      <TextView         android:id="@+id/title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />     <Button         android:id="@+id/request"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Request SafetyNet reCAPTCHA API"/>     <TextView         android:id="@+id/result"         android:layout_width="match_parent"         android:layout_height="wrap_content" /> </LinearLayout>