Chức năng login with google+ api

 

oogle OAuth API provides an easy and powerful way to integrate the login system on the website. Google Login API allows the user to sign into the website using their Google account without sign up on that website. Google login system definitely helps to increase the subscribers on your website. Because nowadays almost all users have a Google account and they can log in with their Google account without registration on your website.

The web developers can easily implement login and registration system in the web application using Google OAuth 2.0 and PHP. In this tutorial, we’ll show how you can integrate user login system with Google authentication using Google API PHP library. Here we’ll provide the step-by-step guide to implementing login with Google account using PHP and store the user information in the MySQL database. Our example Google login script uses the API PHP Client Library to implement Login with Google using PHP in the web application.

Before getting started to integrate Login with Google using PHP and MySQL, take a look at the files structure.

Create Google API Console Project

  • Go to the Google API Console.
  • Select an existing project from the projects list, or click NEW PROJECT to create a new project:
    • Enter the Project Name.
    • Under the Project Name, you will see the Google API console automatically creates a project ID. Optionally you can change this project ID by the Edit link. But project ID must be unique worldwide.
    • Click on the CREATE button and the project will be created in some seconds.
  • In the left side navigation panel, select Credentials under the APIs & Services section.
  • Select the OAuth consent screen tab, specify the consent screen settings.
    • In Application name field, enter the name of your Application.
    • In Support email filed, choose an email address for user support.
    • In the Authorized domains, specify the domains which will be allowed to authenticate using OAuth.
    • Click the Save button.
  • Select the Credentials tab, click the Create credentials drop-down and select OAuth client ID.
    • In the Application type section, select Web application.
    • In the Authorized redirect URIs field, enter the redirect URL.
    • Click the Create button.

A dialog box will appear with OAuth client details, note the Client ID and Client secret. This Client ID and Client secret allow you to access the Google APIs.

google-api-console-project-php-oauth-client-id-secret-codexworld

Note that: This Client ID and Client secret need to be specified in the script at the time of Google API call. Also, the Authorized redirect URI needs to be matched with the redirect URL that specified in the script.

Do you want a detailed guide on Google Application creation? Go through this guide to create Google API Console Project and get Client ID & Client secret.

Create Database Table

To store the user account information from Google, a table needs to be created in the database. The following SQL creates a users table with some basic fields in the MySQL database to hold the Google profile information.

 

 

Google API Client Library for PHP

The google-api-php-client/ directory contains the Google OAuth Library for PHP. The composer is not required to install Google API PHP Client, it can be used without using composer. You don’t need to download it separately, all the required files of Google API Library are included in our Google Login PHP source code.

User Class (User.class.php)

The User class handles the database related operations (connect, insert, and update) using PHP and MySQL. It helps to connect to the database and insert/update Google account data in the users table.

  • __construct() – Connect to the MySQL database.
  • checkUser() – Insert or update the user data based on the OAuth provider and ID. Returns the account data of a specific user as an array.

 

 

Site Settings and API Configuration (config.php)

In the config.php file, database settings and Google API configuration constant variables are defined.
Database constants:

  • DB_HOST – Specify the database host.
  • DB_USERNAME – Specify the database username.
  • DB_PASSWORD – Specify the database password.
  • DB_NAME – Specify the database name.
  • DB_USER_TBL – Specify the table name where the user’s account data will be stored.

Google API constants:

  • GOOGLE_CLIENT_ID – Specify the Google Project Client ID.
  • GOOGLE_CLIENT_SECRET – Specify the Google Project Client Secret.
  • GOOGLE_REDIRECT_URL – Specify the Callback URL.

Call Google API:
The Google Client library is used to connect with Google API and working with OAuth client.

 

 

 

Note that: You’ll find the Client ID and Client Secret on the Google API Manager page of the API Console project.

Login & Get Google Account Data (index.php)

In this file, the API authentication and authorization process are handled using PHP.

  • Initially, The login URL is generated for authentication and Google Sign-in button is shown to the user.
  • If the user authenticates with their Google account, the following happens:
    • The profile information is retrieved from the Google account.
    • The account data is inserted into the database using checkUser() function of User class.
    • The user’s account info is stored in the SESSION.
    • The Google account information (name, email, gender, locale, profile picture, and profile link) is displayed on the webpage.

 

 

Logout (logout.php)

When the user wishes to log out from their Google account, the logout.php file is loaded.

  • Remove token and user data from the SESSION.
  • Reset OAuth access token.
  • Destroy the entire session data.
  • Redirect the user to the homepage.

 

 

Conclusion

We’ve tried to make Google Login integration process quicker and easier. The example code integrates Google Login with the Google API Client for PHP. Also, you don’t need to add API Client Library files separately, our source code contains all the required files with the OAuth client for PHP. If you want to provide a user-friendly login interface, integrate Google Login without page refresh using JavaScript API – Login with Google Account using JavaScript

 

Nguồn: https://www.codexworld.com/login-with-google-api-using-php/

Add Comment