When I try to integrate Google Authentication in my project. I'm facing 403 Forbidden error.
With message Forbidden You don't have permission to access this resource.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
I am using PHP Codeigniter and this is my code below
$client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
$client_secret = '###########################';
$google_client = new Google\Client();
$google_client->setClientId($client_id);
$google_client->setClientSecret($client_secret);
// $google_client->setRedirectUri(base_url('/'));
// $google_client->setRedirectUri('https://test.com/beta/');
$google_client->setRedirectUri('https://test.com/beta/user/login');
$google_client->addScope('email');
$google_client->addScope('profile');
if (isset($_GET["code"])) {
$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]);
if (!isset($token["error"])) {
$google_client->setAccessToken($token['access_token']);
$this->session->set_userdata('access_token', $token['access_token']);
$google_service = new Google\Service\Oauth2($google_client);
$data = $google_service->userinfo->get();
$userId=$data->id;
$userEmail= $data->email;
$userName=$data->name;
}
}
$login_button = '';
if (!$this->session->userdata('access_token')) {
$login_button = '<a href="' . $google_client->createAuthUrl() . '"><img class="border p-2 rounded-pill" style="width:3rem;" src="' . base_url("assets/web/image/google_logo.png") . '" /></a>';
}
It will be really helpful if anyone give me a solution.