Ravi Ghadiya
Ravi Ghadiya

Reputation: 51

How to implement Google reCAPTCHA v3 in an Android app?

I'm building an Android app and I want to implement Google reCAPTCHA v3 to prevent bots from submitting forms. I've already implemented reCAPTCHA on my website for web and server side, but I'm not sure how to do it in my Android app.

Can someone help me with the steps to implement Google reCAPTCHA v3 in an Android app? I would appreciate any sample code or tutorials that you can recommend.

Thank you in advance for your help!

I have gone through lots of websites and blogs to find the specific documentation or demo to implement the reCAPTCHA v3 in android app. but not finding any useful resources which could help in fact.

Upvotes: 5

Views: 9304

Answers (2)

Yogendra
Yogendra

Reputation: 5288

Available for mobile apps too.

https://cloud.google.com/recaptcha-enterprise/docs/instrument-android-apps

Dependency :

implementation 'com.google.android.recaptcha:recaptcha:18.2.1'

Manifest :

<manifest ...>

<uses-permission android:name="android.permission.INTERNET" />

<application ...>
...

Full code :

class CaptchaActivity : AppCompatActivity() {

    private lateinit var recaptchaClient: RecaptchaClient


    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_captcha)
        initializeRecaptchaClient()


        findViewById<Button>(R.id.btn_click_login).setOnClickListener {
            executeLoginAction()
        }
    }

    private fun initializeRecaptchaClient() {
        lifecycleScope.launch {
            Recaptcha.getClient(application, getString(R.string.re_captcha_key))
                .onSuccess { client ->
                    showToast("initializeRecaptchaClient success")
                    recaptchaClient = client
                }
                .onFailure { exception ->
                    showToast("initializeRecaptchaClient failure exception: ${exception.message}")
                }
        }
    }

    private fun executeLoginAction() {
        lifecycleScope.launch {
            recaptchaClient
                .execute(RecaptchaAction.LOGIN)
                .onSuccess { token ->
                    if (token.isNotBlank()) {
                        showToast("Ready for login >> Token $token")
                    } else {
                        showToast("Not ready for login >> Token $token")
                    }
                }
                .onFailure { exception ->
                    showToast("executeLoginAction >> failure >> Exception :${exception.message}")
                }
        }
    }
}

Upvotes: 0

SyncroIT
SyncroIT

Reputation: 1578

It seems like ReCaptcha v3 is only available for web, and not native mobile apps.

I found an alternative called ReCaptcha Enterprise that you can check by yourself here: https://cloud.google.com/recaptcha-enterprise/docs/instrument-android-apps

As you can see, even Big G says it:

"This page explains how to integrate reCAPTCHA Enterprise in your Android app. Due to the variation in mobile devices in terms of screen size, performance, and UIs of the apps, the visual checkbox reCAPTCHA challenge (I'm not a robot) is not available for mobile apps. You can instead implement your own tiered enforcement strategy, such as an MFA flow to provide an alternative redemption path for suspicious traffic."

Upvotes: 0

Related Questions