Getting reCaptcha Android

Viewed 100

I trying to get reCaptcha-code for send to Firebase server. Read here, implement Executor to Activity and write this code at onClick:

override fun onClick(view: View?) {
        SafetyNet.getClient(this).verifyWithRecaptcha("Here i write my API Key")
            .addOnSuccessListener(this as Executor, OnSuccessListener { response ->
                val userResponseToken = response.tokenResult
                Log.d("TAG", userResponseToken.toString())
            })
            .addOnFailureListener(this as Executor, OnFailureListener { e ->
                if (e is ApiException) {
                    Log.d("TAG", "Error: ${CommonStatusCodes.getStatusCodeString(e.statusCode)}")
                } else {
                    Log.d("TAG", "Error: ${e.message}")
            })

Logs are not printing when I click button and call onClick method, but this method works. Tell me please, what I do wrong?

1 Answers

I have tested this and it works without using the as Executor like the below example:

SafetyNet.getClient(this)
    .verifyWithRecaptcha("MY API KEY")
    .addOnSuccessListener(this, OnSuccessListener { response ->
        // Indicates communication with reCAPTCHA service was successful.
        val userResponseToken = response.tokenResult
        if (response.tokenResult?.isNotEmpty() == true) {
            // Validate the user response token using the
            // reCAPTCHA siteverify API.
        }
    })
    .addOnFailureListener(this, OnFailureListener { e ->
        if (e is ApiException) {
            // An error occurred when communicating with the
            // reCAPTCHA service. Refer to the status code to
            // handle the error appropriately.
            Log.d("TAG", "Error: ${CommonStatusCodes.getStatusCodeString(e.statusCode)}")
        } else {
            // A different, unknown type of error occurred.
            Log.d("TAG", "Error: ${e.message}")
        }
    })
Related