Reputation: 41
I'm using Google recaptcha api to implement captcha on login action in my app. But every time I try to initialise the recaptcha api to get the recaptcha tasks client then It's giving either INTERNAL_ERROR or Timed out waiting for 10000 ms
I'm calling initiateSDK inside onCreate() of Application activity
Please let me know what's the reason for these errors and solution to fix.
In iOS, we are using same approach.
Code Snippet
public void initiateSDK(Context context) {
if (recaptchaTasksClient == null) {
init(context);
}
}
private void init(Context context) {
new Handler().post(() -> Recaptcha.getTasksClient((Application) context.getApplicationContext(), BuildConfig.RECAPTCHA_API_KEY)
.addOnSuccessListener(client -> {
recaptchaTasksClient = client;
})
.addOnFailureListener(e -> {
e.printStackTrace();
Log.d("RECAPTCHA", e.getMessage());
}));
}
Upvotes: 4
Views: 2804
Reputation: 155
I experienced this. What ended up being the issue was that the client initialization insisted on being done on a UI thread. The working code ended up looking something like this:
private fun initializeRecaptchaClient() {
lifecycleScope.launch {
withContext(Dispatchers.Main) {
Recaptcha.getClient(application, "SITE_KEY")
.onSuccess {
this.recaptchaClient = it
}
.onFailure {
Log.e("Error initializing Recaptcha client", it)
}
}
}
}
The error that led to trying this solution, which only intermittently showed up was:
E/ViewConfiguration: Tried to access UI constants from a non-visual Context:[overridden Application class]@870795fUI constants, such as display metrics or window metrics, must be accessed from Activity or other visual Context. Use an Activity or a Context created with Context#createWindowContext(int, Bundle), which are adjusted to the configuration and visual bounds of an area on screen java.lang.IllegalArgumentException: Tried to access UI constants from a non-visual Context:[overridden Application class]@870795f
I'm very much not entirely sure why that is. I did get the client to initialize successfully in a brand new test project without needing to force to the Main thread.
Although another cause of the "Internal Error" that I experienced while trying to get it working in the new app was forgetting the INTERNET
permission.
I hope this helps someone.
Upvotes: 1