I am developing a mobile application with googleplus login. where I get the access token and send it to my server in order to verify it using Google APIs Client Library for PHP.
When I try to authenticate the token I got a Fatal Error Saying :
Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_client'' in C:\xxx\vendor\google\apiclient\src\Google\Auth\OAuth2.php:127 Stack trace: #0 C:\xxx\vendor\google\apiclient\src\Google\Client.php(130): Google_Auth_OAuth2->authenticate('ya29.2QBihTdAag...') #1 C:\xxx\index.php(20): Google_Client->authenticate('ya29.2QBihTdAag...') #2 {main} thrown in C:\xxx\vendor\google\apiclient\src\Google\Auth\OAuth2.php on line 127
and here is my PHP code:
<?php
$google_client_id = 'XXXXX.apps.googleusercontent.com';
$google_client_secret = 'XXXXX';
$google_redirect_url = 'xxx';
$google_developer_key = 'XXXXXXXX';
$google_application_name = 'XXXX Login';
$google_application_scope = 'email'; /* I only needed the basic user info */
$google_redirect_uri='';
//include google api files
require_once 'vendor/autoload.php';
$gClient = new Google_Client();
$gClient->setApplicationName($google_application_name);
$gClient->setClientId($google_client_id);
$gClient->setClientSecret($google_client_secret);
$gClient->setRedirectUri($google_redirect_uri);
$gClient->setScopes($google_application_scope);
$gClient->setDeveloperKey($google_developer_key);
$gClient->authenticate("ya29.2QBihTdAagwpH5hHJUg5soIETLspVUgle6VPOT52UYPWkOKyHdUJlpUyvoMKPgkuqIS4PvgXQhfpHw");//Error Happened here
$token = json_decode($gClient->getAccessToken());
$google_oauthV2 = new Google_Service_Oauth2($gClient);
$user_info = $google_oauthV2->userinfo->get();
var_dump(user_info);
die();
?>
For $google_client_secret
, instead of 'notasecret'
use the value in the "CLIENT SECRET"
section of your "Client ID for web application"
.