Reputation: 31300
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.
Upvotes: 0
Views: 565
Reputation: 31300
I was finally able to get the example code from Google to run. I think the problem was that I was using the wrong Client ID and Client Secret. Well, actually I was using the wrong Client ID Credential application type all together. I think I was using the web application
for an installed application
. Well, I'm glad it was my error, and not a problem with the API. The Native Application
is for the Installed Application
. I guess those terms get used interchangeably. And the Installed Application
is for running PHP from the Command Line
. The other thing I noticed, is that when the Native Application
client ID is created, you can NOT change the Redirect URIs. They are automatically set to:
urn:ietf:wg:oauth:2.0:oob
http://localhost
Upvotes: 1