1<?php
2require_once 'vendor/autoload.php';
3
4// init configuration
5$clientID = '<YOUR_CLIENT_ID>';
6$clientSecret = '<YOUR_CLIENT_SECRET>';
7$redirectUri = '<REDIRECT_URI>';
8
9// create Client Request to access Google API
10$client = new Google_Client();
11$client->setClientId($clientID);
12$client->setClientSecret($clientSecret);
13$client->setRedirectUri($redirectUri);
14$client->addScope("email");
15$client->addScope("profile");
16
17// authenticate code from Google OAuth Flow
18if (isset($_GET['code'])) {
19 $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
20 $client->setAccessToken($token['access_token']);
21
22 // get profile info
23 $google_oauth = new Google_Service_Oauth2($client);
24 $google_account_info = $google_oauth->userinfo->get();
25 $email = $google_account_info->email;
26 $name = $google_account_info->name;
27
28 // now you can use this profile info to create account in your website and make user logged in.
29} else {
30 echo "<a href='".$client->createAuthUrl()."'>Google Login</a>";
31}
32?>
33