Google ile oturum açmayı PHP'de ayarlamak için aşağıdaki adımları takip edebilirsin:

1. Google OAuth 2.0 Kimlik Bilgilerini Al

Google Cloud Console adresine giderek bir proje oluştur.

“Kimlik Bilgileri” sekmesine gir ve “OAuth 2.0 İstemci Kimliği” oluştur.

Yetkili yönlendirme URI'sini (örneğin: http://localhost/callback.php) eklemeyi unutma.


2. Google API PHP Kütüphanesini Kur

Google API istemcisi için aşağıdaki komutu kullan:

composer require google/apiclient

3. PHP Kodu ile Google Girişini Ayarla

config.php (Google API yapılandırması)

require 'vendor/autoload.php';

$client = new Google_Client();
$client->setClientId('GOOGLE_CLIENT_ID');
$client->setClientSecret('GOOGLE_CLIENT_SECRET');
$client->setRedirectUri('http://localhost/callback.php');
$client->addScope('email');
$client->addScope('profile');

index.php (Giriş Butonu)

<?php
require 'config.php';
$login_url = $client->createAuthUrl();
?>
<a href="<?= $login_url ?>">Google ile Giriş Yap</a>

callback.php (Google'dan Dönüş)

<?php
require 'config.php';

if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($token);

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

// Kullanıcı bilgileri
echo "Ad: " . $userInfo->name . "<br>";
echo "E-posta: " . $userInfo->email;
}