Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering the Google Recaptcha in Android Studio 3

I am using Android Studio 3

I am following this article to learn how to use Google Recaptcha in Android Studio.

Installed the package using this: implementation 'com.google.android.gms:play-services-safetynet:12.0.1'

API keys are also registered.

I saw there is onClick event handler but where is it mentioned about rendering the recaptcha?

Update 1

When I wrote the button click code as mentioned in the link...I got a complication error: inconvertible types cannot cast anonymous android.view.view.onclicklistener to java.util.concurrent.executor

Code as asked in comment

btn_Login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View view) {
        SafetyNet.getClient(this).verifyWithRecaptcha("")
            .addOnSuccessListener((Executor) this,
                new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
                    @Override
                    public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
                        // Indicates communication with reCAPTCHA service was
                        // successful.
                        String userResponseToken = response.getTokenResult();
                        if (!userResponseToken.isEmpty()) {
                            // Validate the user response token using the
                            // reCAPTCHA siteverify API.
                        }
                    }
                })
            .addOnFailureListener((Executor) this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    if (e instanceof ApiException) {
                        // An error occurred when communicating with the
                        // reCAPTCHA service. Refer to the status code to
                        // handle the error appropriately.
                        ApiException apiException = (ApiException) e;
                        int statusCode = apiException.getStatusCode();

                    } else {
                    }
                }
            });
    }
});
like image 668
Pankaj Avatar asked Apr 16 '18 19:04

Pankaj


1 Answers

I used below code and everything is work fine now.

Make sure to implement Executor in the activity

btn_Login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View view) {
        SafetyNet.getClient(Activity.this).verifyWithRecaptcha("")
            .addOnSuccessListener((Activity) MyActivity.this,
                new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
                    @Override
                    public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
                        // Indicates communication with reCAPTCHA service was
                        // successful.
                        String userResponseToken = response.getTokenResult();
                        if (!userResponseToken.isEmpty()) {
                            // Validate the user response token using the
                            // reCAPTCHA siteverify API.
                        }
                    }
                })
            .addOnFailureListener((Activity) MyActivity.this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    if (e instanceof ApiException) {
                        // An error occurred when communicating with the
                        // reCAPTCHA service. Refer to the status code to
                        // handle the error appropriately.
                        ApiException apiException = (ApiException) e;
                        int statusCode = apiException.getStatusCode();

                    } else {
                    }
                }
            });
    }
});
like image 100
Pankaj Avatar answered Sep 21 '22 14:09

Pankaj