Reputation: 1441
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).
Upvotes: 20
Views: 8932
Reputation: 1100
You can add Google reCaptcha in your android application with the help of SafetyNet google library it provides Google reCaptcha API.
Add SafetyNet library in your android project
implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
https://gist.github.com/anehkumar/dec49c972999fd8e891d408c38a6b532
For complete tutorial please check this post:- https://trinitytuts.com/add-google-recaptcha-in-android-application/
Upvotes: 0
Reputation: 185
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.
Upvotes: 2
Reputation: 1591
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.
repositories {
jcenter()
}
dependencies {
compile 'android.lib.recaptcha:reCAPTCHA:+'
}
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);
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
.
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
.
You can force the widget to render in a specific language. Please refer to this page.
reCaptcha.setLanguageCode("fr");
Upvotes: 2