Little update, I think I found the root of the problem.
I changed the code that makes the request in the original question with a simple cURL POST call which is as follows:
// Get the oAuth code.
$authCode = $request->code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://accounts.google.com/o/oauth2/token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $authCode,
'client_id' => $this->clientCredentials['client_id'],
'client_secret' => $this->clientCredentials['client_secret'],
'redirect_uri' => $this->callbackUrl,
'grant_type' => 'authorization_code'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
When I changed this, I got the following JSON response which is the same error:
{
"error" : "invalid_request"
}
Then I added the following lines to update the POST request headers:
$headers = array(
'Content-Type: application/x-www-form-urlencoded'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
And after this, I got the following JSON response, which had the error description you mentioned:
{
"error" : "invalid_request",
"error_description" : "Required parameter is missing: grant_type"
}
Then I did some googling, and found the solution to change the line of code that sets the post fields I provided above with the method "http_build_query" added to the post fields array.
Complete code is this:
// Get the oAuth code.
$authCode = $request->code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://accounts.google.com/o/oauth2/token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'code' => $authCode,
'client_id' => $this->clientCredentials['client_id'],
'client_secret' => $this->clientCredentials['client_secret'],
'redirect_uri' => $this->callbackUrl,
'grant_type' => 'authorization_code'
)));
$headers = array(
'Content-Type: application/x-www-form-urlencoded'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
After this change, I was able to get the tokens without any problem.
I just changed the request method in order to fix this, I did not change anything about my client credentials, and I also used the API example files to test the previous cases and all were getting the "invalid_request" response without any description, even if I test with native client credentials. Therefore, I believe that the error may arise because of the AdWords PHP client library.
We did some checks and according to our commit history, we did not change even a line in our previous method, which worked with the AdWords PHP library methods flawlessly for months, then started to give the error "invalid_request". I think there is something wrong with the PHP library, and I would like to use the library methods instead of this cURL solution, which is not well structured like the library methods. As soon as the error is fixed, I would like to go back to my previous methods.
I don't know if this is just for our case, but I hope it does not happen to anybody else.
Looking forward to hear from you.
Thanks,
Burak.