Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Integrate reCAPTCHA 2.0 in Android

Is there any way I can integrate reCAPTCHA 2.0 in Android? I found this library and got it working. However, server side verification of the CAPTCHA is not supported (it needs me to provide the private key in the code then verify it within the app instead of talking to my own server).

  1. Is there a way to integrate reCAPTCHA 2.0 in Android?
  2. Or is there a way for me to verify the CAPTCHA on my own server with that library?
like image 949
Lancelot Avatar asked Jan 04 '16 10:01

Lancelot


People also ask

How do I integrate v2 reCAPTCHA?

Very first thing you need to do is register your website on Google recaptcha to do that click here. Login to your Google account and create the app by filling the form. Select the reCAPTCHA v2 and in that select “I am not a robot” checkbox option. Once submit, Google will provide you following two information.

How do I use Google reCAPTCHA on Android?

In the Adding reCAPTCHA to your app section on the page that appears next, your public and private keys appear under Site key and Secret key, respectively. You use the site key when you send the verify request, and you use the secret key when you validate the user response token.

How do I add reCAPTCHA to app?

To add the reCAPTCHA Enterprise API dependency, add the following build rule to the dependencies section of your app-level build. gradle file. For more information about API dependencies in Android apps, see Set Up Google Play Services.


2 Answers

Fork this android library and modify the server-side logic : https://github.com/ayltai/Android-Lib-reCAPTCHA

The reCAPTCHA Android Library provides a simple way to show a CAPTCHA as an ImageView in your Android app, helping you stop bots from abusing it. The library wraps the reCAPTCHA API.

Installation

repositories {
    jcenter()
}

dependencies {
    compile 'android.lib.recaptcha:reCAPTCHA:+'
}

The Layout

To show CAPTCHA image, you need to add a <android.lib.recaptcha.ReCaptcha /> element to your layout XML:

<android.lib.recaptcha.ReCaptcha
    android:id="@+id/recaptcha"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerInside" />

It is important to use android:scaleType="centerInside" to ensure the entire CAPTCHA image can be displayed.

Alternatively, you can create an instance of android.lib.recaptcha.ReCaptcha at runtime:

ReCaptcha reCaptcha = new ReCaptcha(context);

How to show CAPTCHA

In your activity/fragment/view containing android.lib.recaptcha.ReCaptcha, you need display a CAPTCHA image for the user to response:

ReCaptcha reCaptcha = (ReCaptcha)findViewById(R.id.recaptcha);
reCaptcha.showChallengeAsync("your-public-key", onShowChallengeListener);

showChallengeAsync downloads and shows CAPTCHA image asynchronously. It is safe to invoke in UI thread. No exception will be thrown in case of any error by this call. All errors will be treated as unsuccessful in showing CAPTCHA image.

onShowChallengeListener is an instance of ReCaptcha.OnShowChallengeListener, which is called when an attempt to show a CAPTCHA is completed.

The synchronous version of this method is showChallenge.

How to verify user input

To verify user input, pass the input string to ReCaptcha.verifyAnswerAsync (or ReCaptcha.verifyAnswer):

reCaptcha.verifyAnswerAsync("your-private-key", "user-input", onVerifyAnswerListener);

verifyAnswerAsync asynchronously submits the user input string to reCAPTCHA server for verification. It is safe to invoke in UI thread. No exception will be thrown in case of any error by this call. All errors will be treated as verification failure.

onVerifyAnswerListener is an instance of ReCaptcha.OnVerifyAnswerListener, which is called when an attempt to verify the user input is completed.

The synchronous version of this method is verifyAnwser.

Specify a locale

You can force the widget to render in a specific language. Please refer to this page.

reCaptcha.setLanguageCode("fr");
like image 98
compte14031879 Avatar answered Sep 29 '22 03:09

compte14031879


One approach would be to create an HTML file with a working reCaptcha 2.0 form (reCAPTCHA Docs) and host that on a web site (make it responsive so it looks nice).

then load the URL on a WebView and make a bridge so you can interact between Java and Javascript (addJavascriptInterface)

Android Activity:

WebView mWebView = (WebView) findViewById(R.id.webview);

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("http://url/to/recaptcha/file/index.html");
mWebView.addJavascriptInterface(new BridgeWebViewClass(this), "BridgeWebViewClass");

Bridge Class:

public class BridgeWebViewClass {

    @JavascriptInterface
    public void reCaptchaCallbackInAndroid(String g_response){
        log.d("reCaptcha", "token" + g_response);
    }
}

And now from your HTML file you can run the Bridge Class as a Javascript function:

<div class="g-recaptcha" data-sitekey="YOUR_CAPTCHA_SITE_KEY" data-callback="captchaResponse"></div>

<script type="text/javascript">
    function captchaResponse(token){
        BridgeWebViewClass.reCaptchaCallbackInAndroid(token);
    }
</script>

Now you can verify the response from Android calling https://www.google.com/recaptcha/api/siteverify

Hope this helps.

like image 42
Etienne GT Avatar answered Sep 29 '22 02:09

Etienne GT