Integrate Google Sign In Into Android

Integrate Google Sign Into Android:-

We will integrate Google sign in into  Android app. Google let users sign in through their Gmail account in their Android app.Google signing we get all the user details such as profile details, image, contacts very easily and quickly.

Integrating Google sign in into your Android app lets users sign quickly without the need for the complex registration process.



Configure Google Sign in:-

In order to integrate Google Sign In we need to first go ahead and set up the project on the Google developer console. We need to add  google-services.json file to your project’s app folder. Please visit on the link here.

  1. This will take you to link where you will be prompted to enter app name and package name as shown below.
  2. Enter the project name if not create a project.
  3. Then enter the project package name.
  4. Click on choose and now we get the next screen where we need to add our SHA1 key.

Generate SHA1 Key :-

Now we need Java keytool to generate SHA1 fingerprint so, in order to use Java keytool, you must have Java installed on your machine.Then you go to your command prompt and type the following command below to generate the SHA1 fingerprint.

You will get something like this as output:-

Copy SHA1 value and paste in the next screen.

Go to next and Click on download google-services.json and download the JSON file.

Create and Setup Project :

Create the New project and Enter same package name as you have configured before.

Add com.google.android.gms:play-services-auth:11.0.2 dependency to your app level build file and apply plugin ‘com.google.gms.google-services‘ on the bottom of your app level build folder.

Open project level build file and add following

Create activity_main.xml:-

we are adding two buttons one for SignIn and other for SignOut.

Configure Auth2.0 Client Id :-

you can skip this if you want to do signin just for demo purpose but, if you want to do a backend validation we need google signin to generate a token and send to our REST server which will do the validations and authentication.  This step is needed if you want to pass the currently signed-in user to a backend server, send the ID token to your backend server and validate the token on the server.Visit Google developer console  . Select the appropriate project and click on Credentials.

You see list of  OAuth 2.0 client IDs, select Web client (Auto-created for Google Sign-in) and copy the client id and store it in a string resource as

We are using this key when we create an object of  GoogleSignInOptions  as below in MainActivity.java

Now Open your MainActivity.java and add below set of code step by step.

In OnCreate Method add GoogleSigninoptions object create a method called signinClick() ,handleSignInResult().

MainActivity.java:-

We are receiving token after the user signed in in handleSignInResult and now use this token to validate. Please visit the link here to check google implementation here .

Output:-

Download Code

Be the first to comment

Leave a Reply

Your email address will not be published.


*