Asked 6 years ago
8 Feb 2018
Views 3160
jaggy

jaggy posted

google plus Oauth 2.0 give error at callback url in php

using google plus login . and it working good until it return at call back with ?code=4/yTOGmqs-utkNDXPViyxYAthHjppYiSreH
and i got error at callback

 America/Denver] PHP Fatal error:  Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'' in google/auth/Google_OAuth2.php:113
Stack trace:
#0  /google/Google_Client.php(131): Google_OAuth2->authenticate(Array, NULL)



if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  header('Location: http://' . $_config['returnUrl']);
}
Mitul Dabhi

Mitul Dabhi
answered Apr 25 '23 00:00

Note that the Google Plus API has been deprecated and is no longer available. If you're looking to implement OAuth 2.0 with Google, you can use the Google Sign-In API instead. Here are the general steps for implementing Google Sign-In in PHP:

1.Create a Google Cloud Platform project and enable the Google Sign-In API.
2.Obtain a client ID and client secret from the Google Cloud Console.
3.Set up the Google API client library for PHP in your project.
4.Use the Google API client library to authenticate the user and obtain an access token.
5.Use the access token to access Google APIs on behalf of the user.
Google provides a step-by-step guide for implementing Google Sign-In with PHP in their documentation: https://developers.google.com/identity/sign-in/web/sign-in

By following these steps, you can securely authenticate users with Google and access their data using OAuth 2.0.
Post Answer