EDIT This particular error was from my mistake of not setting up the application correctly. It's a situation where, even though the code works now, I have no idea what caused the problem, and I'd need to get deep into the inner workings to figure it out. end
I'm running the Google Drive API QuickStart example code for PHP from Windows7 operating system command line. Here is the code that I am running:
I can get through obtaining an authorization URL, that will successfully return my access token. But after entering the access token, something goes wrong in the `Google_OAuth2.php file supplied by Google to complete the authorization.
Here is a more complete error msg:
Fatal error:
Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token,
message: 'invalid_request'' in C:\google-api-php-client\src\auth\Google_OAuth2.php:115 Stack trace:
#0 C:\google-api-php-client\src\Google_Client.php(127): Google_OAuth2->authenticate(Array, '4/HW2t_MZIxatq6...')
#1 C:\Users\Gigabyte\MyWebsite\Take_Validation.php(10): Google_Client->authenticate('4/HW2t_MZIxatq6...')
#2 {main} thrown in C:\google-api-php-client\src\auth\Google_OAuth2.php on line 115
This is part of the code from the Google_OAuth2.php
file: Line 115 is the last statement throw new Google_AuthException
/**
* @param $service
* @param string|null $code
* @throws Google_AuthException
* @return string
*/
public function authenticate($service, $code = null) {
if (!$code && isset($_GET['code'])) {
$code = $_GET['code'];
}
if ($code) {
// We got here from the redirect from a successful authorization grant, fetch the access token
$request = Google_Client::$io->makeRequest(new Google_HttpRequest(self::OAUTH2_TOKEN_URI, 'POST', array(), array(
'code' => $code,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirectUri,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret
)));
if ($request->getResponseHttpCode() == 200) {
$this->setAccessToken($request->getResponseBody());
$this->token['created'] = time();
return $this->getAccessToken();
} else {
$response = $request->getResponseBody();
$decodedResponse = json_decode($response, true);
if ($decodedResponse != null && $decodedResponse['error']) {
$response = $decodedResponse['error'];
}
throw new Google_AuthException("Error fetching OAuth2 access token, message: '$response'", $request->getResponseHttpCode());
}
}
Obviously the error comes before line 115. Line 115 is just printing out that an error occurred somewhere before. I don't know if it's the code, or something I'm doing wrong. I know that every time a new URL authorization request is put in, I get a different access code back. My client secret and client ID shouldn't ever need to change. I have no idea how to make this work at this point.