How to Integrate Google reCAPTCHA in Android?
Google reCAPTCHA is one of the services provided by Google which is used to verify whether the user is a bot or not. The Google reCAPTCHA is seen in many websites and applications to verify the users. In this article, we will take a look at the implementation of Google reCAPTCHA in Android.
What we are going to build in this article?
We will be building a simple application in which we will be displaying a Google reCAPTCHA verify user button, after clicking that button we will display the Google reCAPTCHA to our user and verify them. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Working of Google reCAPTCHA
While using reCAPTCHA it makes several calls from your application to the Safety Net server and from the Safety Net server to your application. So you can get to know about these calls in more detail in the below diagram.
Steps in which we make API calls:
- For using reCAPTCHA in your application we have to generate a Site key and Secret Key which we have to add to our application. The site key is used in our Android application and the secret key is stored on the server.
- With the help of this site, key reCAPTCHA will be generated and it will verify if the user is a robot or not.
- After making verification by reCAPTCHA our application will communicate with our captcha server and it will return a response using your site key.
- Now our application will send a token to our server and our server will then send a token to the reCAPTCHA server with our Secret key. Then reCAPTCHA server will send a success response to our server and the server will send a success response to our application.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Adding the dependency for volley and Safety Net
As we will be using the API provided by Google. So for using this API we will be using Volley for handling our HTTP requests and a safety net for connecting to Google reCAPTCHA.
After adding this dependency now sync your project and now we will move towards the creation of our API key which we will require for Google reCAPTCHA.
Step 3: Generating API key for using Google reCAPTCHA
For using Google reCAPTCHA we have to build two keys such as site key and site secret key which we have to use for authentication. For creating a new API key navigate to this Google developers site. And refer to the following diagram to generate the API keys.
After adding this data accept reCAPTCHA terms and then click on Submit option.
Step 4: Adding permissions for the Internet
As we are calling the API for Google reCAPTCHA so we have to add permissions for the Internet in our AndroidManifest.xml. Navigate to the app > AndroidManifest.xml and add the below code to it.
Step 5: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 6: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
After adding this code make sure to add the keys which we have generated inside your app. After adding the keys run your app and see the output of the app.