John Davidson

oauth 2.0 - Google_Service_OAuth2 is 'Undefined type' in PHP

0 comments
Message:


I'm trying to login with Google Sign-in but it's showing the 'Google_Service_OAuth2' as an undefined type.


I found this stack post: Google_Service_Oauth2 is undefined but there were no responses on it.


I installed the apiclient with composer and after research I've found that each of the files are in the correct locations (such as the src folder). Additionally, other libraries I've installed using composer are working properly.


Using command composer require google/apiclient composer installed version 2.12. I've tried installing v2.0 which is the version that most of the guides and posts I've found are using.


I've already run composer dump-autoload and restarted my machine just in case.


Here's my code:


use Google\Client;

$client = new Google_Client();

$clientID = 'MY CLIENT ID';
$clientSecret = 'MY CLIENT SECRET';
$redirectUri = 'http://localhost/dhi-portal/users/login';


$client->addScope('email');
$client->addScope('profile');

session_start();



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

if (!isset($token['error'])) {
$client->setAccessToken($token['accessToken']);

$_SESSION['accessToken'] = $token['accessToken'];

$googleService = new Google_Service_OAuth2($client);

}
}


I haven't continued further from here because it's showing the undefined type.


Any guidance would be greatly appreciated.



My Answer:
a Google API client library that provides access to Google's OAuth 2.0 authentication and authorization services. It allows developers to easily integrate OAuth 2.0 authentication into their applications, enabling users to securely authenticate and authorize access to their Google account data. This library simplifies the process of obtaining access tokens and managing user authentication, making it easier for developers to build secure and reliable applications that interact with Google services.

Rate this post

3 of 5 based on 9908 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us