Apis

Login System with Google using Php and OAuth Api

Google+ Pinterest LinkedIn Tumblr

Nowadays all are feel irritated to fill the long text box of registration form. So they feel simply registered with Google integration. For website  its also Safe to avoid spam and irrelevant data while login using google.  Most of the website demanded this option. social login has become a most integrated one in all login forms. In this tutorial We will learn how to make a Login System with Google using Php and OAuth Api. User will login with their Google account, give permission to access basic profile, and their name with email will be stored in database.

Demo


What is OAuth (Authorization)

Google APIs use the OAuth 2.0 protocol for authentication and authorization. Google supports common OAuth 2.0 scenarios such as those for web server, installed, and client-side applications.

To begin, obtain OAuth 2.0 client credentials from the Google Developers Console. Then your client application requests an access token from the Google Authorization Server, extracts a token from the response, and sends the token to the Google API that you want to access. For an interactive demonstration of using OAuth 2.0 with Google (including the option to use your own client credentials), experiment with the OAuth 2.0 Playground.

Refer – https://developers.google.com/identity/protocols/OAuth2

Simply- your are the owner of your website and you have all admin privileges, ‘Guest’ want to access your page and surf some stuff unless admin grants him with some additional permissions . you can give some permission relevant to ‘Guest’ Role  without sharing your identity and full privileges. This is exactly done by google  OAuth, allow users to Authorize our page with some privileges.

Let start Coding part for Login System with Google using Php and OAuth Api, before that we need to Register our domain with goolge

Get  CLIENT ID and SECRET KEY

Before Coding We need to get CLIENT ID and SECRET KEY for the google, because before the function we need register our website and get CLIENT ID and SECRET KEY for authentication and authorization to our website

Steps for Getting CLIENT ID and SECRET KEY

Step 1 – Go to the Url code.google.com/apis/console
It will redirect to gmail login page after you login with your goole account. It redirect to Google Developer console.

Note – This developer console is everyhting for create Apis Related to the Google

Login System with Google using Php and OAuth Api

Step 2 – Create a New project with your project nameLogin System with Google using Php and OAuth Api

Step 3 – Click APIs & auth link and click credentials and  Oauth 2.0 client Id

credentials

Login System with Google using Php and OAuth Api

Login System with Google using Php and OAuth Api

Step 4 – Before that create Contest screen for product and email verification Login System with Google using Php and OAuth Api

Login System with Google using Php and OAuth Api

Step 5 – Select web Application and Enter you domain and redirect URL details and then get you credentials

Login System with Google using Php and OAuth Api

Finally you get your CLIENT ID and SECRET KEY.

got client id

Coding Step

Create database and table

Create config.php file for configuration with database.. enter Your CLIENT ID and SECRET KEY here and also domain redirect your.

Create redirect URL function google_login.php  and this code for authentication the user with google whether he gave correct credentials or not. if give correct credentials check it ans success page redirect to specified page and insert user data into database.  If it returns error redirect back to index.php. If api returns user details check with database if user email exist and do the necessary accordingly and redirect back to home.php

finally create link for login in index.php

Create welcome page with logout button in home.php

Create logout.php function

That’s it Enjoy the code.

I'm Rajasekar - Web developer, Freelancer, Blogger and Owner of DeveloperDesks. From India lives in Bahrain. I love to do coding, Creating websites and trying different with code and designs. You Can Hire Me