CI3 Google Login)→ Create
emailprofileuserinfo.email & userinfo.profile)→ Save & Continue cho đến xong
http://localhost http://yourdomain.comhttp://localhost/auth/google_callback https://yourdomain.com/auth/google_callback→ Create
Sau khi tạo xong, Google sẽ trả về:
Client ID = xxxxx.apps.googleusercontent.com Client Secret = xxxxxxxx
Ví dụ trong config/constants.php:
define('APP_CLIENT_ID', 'xxxxx.apps.googleusercontent.com');
define('APP_CLIENT_SECRET', 'xxxxxxxx');
define('APP_CLIENT_REDIRECT_URL', 'http://localhost/auth/google_callback');
Hoặc dùng .env nếu bạn có.
public function google_callback()
{
$google = new Google();
if ($this->input->get('code')) {
$token = $google->fetchAccessTokenWithAuthCode(
$this->input->get('code')
);
if (!isset($token['error'])) {
$google->setAccessToken($token['access_token']);
$oauth = new Google_Service_Oauth2($google);
$user = $oauth->userinfo->get();
// $user->email
// $user->name
// $user->picture
}
}
}
❌ redirect_uri_mismatch
→ Redirect URI phải giống 100% (http/https, slash, path)
❌ Error 403: access_denied
→ Quên add email vào Test users
❌ Login chạy local không được
→ Redirect URI phải là:
http://localhost/...
chứ không phải 127.0.0.1