📜  php login google api - PHP (1)

📅  最后修改于: 2023-12-03 15:18:20.331000             🧑  作者: Mango

PHP Login with Google API - Introduction

In this guide, we will learn how to implement login with Google API in a PHP application. The Google API allows users to sign in to your application using their Google credentials, which provides a convenient and secure way of authentication.

Before we begin, make sure you have a Google Developer account and have created a project in the Google Developer Console. You will need the Client ID and Client Secret from your Google project to authenticate users.

Prerequisites
  • PHP programming knowledge
  • Google Developer account with a project set up
  • Client ID and Client Secret from your Google project
Implementation Steps
Step 1: Install Required Libraries

To start, we need to install the required libraries using Composer, a dependency manager for PHP. Open your terminal or command prompt and navigate to your project directory. Run the following command to install the required libraries:

composer require google/apiclient

This will download and install the Google API client library in your project.

Step 2: Configure Google API Credentials

Next, we need to configure the Google API credentials in our PHP application. Create a new PHP file called google_login.php, and add the following code:

<?php
require_once 'vendor/autoload.php';

$client = new Google_Client();
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('http://localhost/google_login_callback.php');
$client->addScope('email');
$client->addScope('profile');

Replace 'YOUR_CLIENT_ID' and 'YOUR_CLIENT_SECRET' with your own Client ID and Client Secret obtained from the Google Developer Console.

Step 3: Implement Login Page

Create a login page (login.php) where users can click a button to initiate the Google login process. Add the following HTML code to the login.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Login with Google</title>
</head>
<body>
    <h1>Login with Google</h1>
    <a href="google_login.php">Click here to login with Google</a>
</body>
</html>
Step 4: Implement Login Callback

Create a callback page (google_login_callback.php) where Google will redirect the user after successful login. Add the following code to the google_login_callback.php file:

<?php
session_start();

$state = $_GET['state'] ?? '';
$code = $_GET['code'] ?? '';

if (!empty($state) && !empty($code)) {
    $token = $client->fetchAccessTokenWithAuthCode($code);
    $_SESSION['access_token'] = $token;
    header('Location: dashboard.php');
    exit;
} else {
    echo 'Failed to authenticate with Google API.';
}
Step 5: Implement Dashboard Page

Create a dashboard page (dashboard.php) where the user will be redirected after successful login. In the dashboard page, you can display the user's information or perform any other necessary operations. Add the following code to the dashboard.php file:

<?php
session_start();

if (!isset($_SESSION['access_token'])) {
    header('Location: login.php');
    exit;
}

$accessToken = $_SESSION['access_token'];
$client->setAccessToken($accessToken);

$googleService = new Google_Service_Oauth2($client);
$userInfo = $googleService->userinfo->get();

echo 'Welcome, ' . $userInfo->name . '! Your email is ' . $userInfo->email;
Step 6: Run the Application

Finally, you can run your PHP application by starting a local server. Navigate to your project directory in the terminal or command prompt and run the following command:

php -S localhost:8000

Open your web browser and go to http://localhost:8000/login.php. Click on the "Login with Google" button, and you will be redirected to the Google login page. After successful login, you will be redirected to the dashboard page, where you can see the user's name and email.

That's it! You have successfully implemented login with Google API in your PHP application.

Note: Make sure to handle error cases and implement proper security measures to protect user data.

Please let me know if you need any further assistance.