PHP : Google plus Oauth 2.0 - Getting Error fetching OAuth2 access token, message: 'invalid_client'

google-api, google-api-php-client, google-oauth, oauth-2.0, php

Solution

Had same issue. Fixed it by deleting the extra space from the client secret. When you copy it from google api console you get an extra space.

Problem

Working on Sign in with Google, using PHP Lib v2, Oauth 2.0. After long time spending on it finally I configured, it worked once fine but after that it is not working. On the clicking of sign in link, it will take user for authentication to Google. Working fine till then authentication done. At the time of callback with `GET` variables: ``` ?code=something&authuser=0&prompt=consent&session_state=something ``` It is generating following error: Fatal error: Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_client'' in C:\xampp\htdocs\test\google-api-php-client\src\auth\Google_OAuth2.php:115 Stack trace: #0 C:\xampp\htdocs\test\google-api-php-client\src\Google_Client.php(127): Google_OAuth2->authenticate(Array, 4/xUGSPbXR0LNzW...') #1 C:\xampp\htdocs\test\google-api-php-client\index.php(40): Google_Client->authenticate('4/xUGSPbXR0LNzW...') #2 {main} thrown in C:\xampp\htdocs\test\google-api-php-client\src\auth\Google_OAuth2.php on line 115 Using following code (it will help you to understand): ``` <?php ini_set('display_errors',1); error_reporting(E_ALL); require_once 'src/Google_Client.php'; require_once 'src/contrib/Google_PlusService.php'; session_start(); $client = new Google_Client(); $client->setApplicationName("Google+ PHP Starter Application"); $plus = new Google_PlusService($client); if (isset($_REQUEST['logout'])) { unset($_SESSION['access_token']); } if (isset($_GET['code'])) { $client->authenticate($_GET['code']); $_SESSION['access_token'] = $client->getAccessToken(); header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); } if (isset($_SESSION['access_token'])) { $client->setAccessToken($_SESSION['access_token']); } if ($client->getAccessToken()) { $me = $plus->people->get('me'); $url = filter_var($me['url'], FILTER_VALIDATE_URL); $img = filter_var($me['image']['url'], FILTER_VALIDATE_URL); $name = filter_var($me['displayName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); $personMarkup = "<a rel='me' href='$url'>$name</a><div><img src='$img'></div>"; $optParams = array('maxResults' => 100); $activities = $plus->activities->listActivities('me', 'public', $optParams); $activityMarkup = ''; foreach($activities['items'] as $activity) { $url = filter_var($activity['url'], FILTER_VALIDATE_URL); $title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); $content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); $activityMarkup .= "<div class='activity'><a href='$url'>$title</a><div>$content</div></div>"; } $_SESSION['access_token'] = $client->getAccessToken(); } else { $authUrl = $client->createAuthUrl(); } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <link rel='stylesheet' href='style.css' /> <body> <header><h1>Google+ Sample App</h1></header> <div class="box"> <?php if(isset($personMarkup)): ?> <div class="me"><?php print $personMarkup ?></div> <?php endif ?> <?php if(isset($activityMarkup)): ?> <div class="activities">Your Activities: <?php print $activityMarkup ?></div> <?php endif ?> <?php if(isset($authUrl)) { print "<a class='login' id='g_pop' href='$authUrl'>Connect Me!</a>"; } else { print_r($me); print "<a class='logout' href='?logout'>Logout</a>"; } ?> </div> </body> </html> ``` Get source files from here: Google APIs Client Library for PHP Already done: - Client ID: OK - Client secret: OK - Redirect URIs: OK - Google+ API: ON Note: Working on localhost.

Original source