PHP - Invalid Request Error in "GetAccessToken"

629 views
Skip to first unread message

Burak Karakan

unread,
Dec 9, 2016, 8:46:03 AM12/9/16
to AdWords API Forum

Hello.

We have been using the AdWords API for months, and implemented an OAuth login feature months ago. We were using it until 2 days ago, but suddenly we started getting the "invalid request" error, which is as follows:




I tried with different client ID and secret couples, but got the same error. I also tried the example codes which are called GetRefreshToken.php and GetRefreshTokenWithoutIniFile.php from the googleads\googleads-php-lib\examples\AdWords\Auth\ path. I tried different things but unable to authenticate myself as the API client.

I am using the following piece of code, where I get the error in the last line:

$user = new AdWordsUser();
$user->SetDeveloperToken(env('DEVELOPER_TOKEN'));
$user->SetUserAgent(env("USER_AGENT"));

$OAuth2Handler = $user->GetOAuth2Handler();
$credentials = $OAuth2Handler->GetAccessToken($this->clientCredentials, $authCode, $this->callbackUrl);

The variable "clientCredentials" contains my client ID and client Secret, "authCode" is the authentication code that came as the parameter to the callback URL, and callback URL is my callback URL. I have been using this setup for more than 3 months, but for the last 2 days it constantly gives me the "invalid request" error.

Thanks for any help. I can provide more details if you want.

Shwetha Vastrad (AdWords API Team)

unread,
Dec 9, 2016, 1:36:27 PM12/9/16
to AdWords API Forum
Hi Burak,

Could you provide the steps you followed to create your OAuth2 credentials and generate the refresh token? Could you also let me know if you are using Installed application type credentials or web app type credentials? The invalid_request error usually occurs if a required parameter is missing from the OAuth2 request. 

Could you also try creating a new set of credentials by following the instructions provided here and let me know if it works? 

Thanks,
Shwetha, AdWords API Team.

Burak Karakan

unread,
Dec 10, 2016, 3:41:45 AM12/10/16
to AdWords API Forum
Hi Shwetha,

We are building a web app, therefore the credentials are for a web app. I always create the credentials using the link you provided. In order to try the examples, I created installed type credentials as well in order to test the example files, but I could not make them work either, got the same error. 

I created different clientID & secret couples but none of them worked. In the error stack, the error seems to be related to this call:

at SimpleOAuth2Handler->MakeRequest('https://accounts.google.com/o/oauth2/token', array('code' => 'my_auth_code_1234567', 'client_id' => 'my_client_id.apps.googleusercontent.com', 'client_secret' => 'my_client_secret', 'redirect_uri' => 'http://mydomain.com/auth/google/callback', 'grant_type' => 'authorization_code'))

I don't know the working version of this call, therefore I don't know what is missing. I can try different things you advise.

Thank you very much for your help.

Burak Karakan

unread,
Dec 12, 2016, 7:51:03 AM12/12/16
to AdWords API Forum
Hi Shwetha, any ideas on this? 

We are unable to run our system for approximately 5 days, any help would be amazing.

Thanks again.

Nadine Sundquist (AdWords API Team)

unread,
Dec 12, 2016, 5:17:45 PM12/12/16
to AdWords API Forum
Hello Burak,

Usually when there is an error in the PHP library where the error is invalid_request, there's also an error_description. Can you see if that's available? It will usually tell you what field is causing the issue such as 'missing required parameter'. That may help us narrow this down.

Thanks,
Nadine, AdWords API Team

Sabri Karagönen

unread,
Dec 13, 2016, 12:53:23 PM12/13/16
to AdWords API Forum
Hi Nadine,

Unfortunately there is no any other description except "Invalid Request"

13 Aralık 2016 Salı 00:17:45 UTC+2 tarihinde Nadine Sundquist (AdWords API Team) yazdı:

Burak Karakan

unread,
Dec 13, 2016, 1:54:16 PM12/13/16
to AdWords API Forum
Hello Nadine.

Sabri is my collegue, we are trying to solve the problem together.

There is no description, I am providing the error stack down below with obsfucating some data.

Thanks.


OAuth2Exception in SimpleOAuth2Handler.php line 119:{
"error" : "invalid_request"
}

  1. in SimpleOAuth2Handler.php line 119
  2. at SimpleOAuth2Handler->MakeRequest('https://accounts.google.com/o/oauth2/token', array('code' => '4/3kDrMBpihSzQ-6qicrTwjRUg1x4YmI_code', 'client_id' => 'my_client_id.apps.googleusercontent.com', 'client_secret' => 'my_client_secret', 'redirect_uri' => 'http://my_domain.com/auth/google/callback', 'grant_type' => 'authorization_code')) in SimpleOAuth2Handler.php line 72
  3. at SimpleOAuth2Handler->GetAccessToken(array('client_id' => 'my_client_id.apps.googleusercontent.com', 'client_secret' => 'my_client_secret'), '4/3kDrMBpihSzQ-6qicrTwjRUg1x4YmI_code', 'http://my_domain.com/auth/google/callback') in AuthController.php line 152


This is the exact error, directly copied and pasted.

Thanks.
Burak

Burak Karakan

unread,
Dec 13, 2016, 3:35:06 PM12/13/16
to AdWords API Forum
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.


On Tuesday, December 13, 2016 at 1:17:45 AM UTC+3, Nadine Sundquist (AdWords API Team) wrote:

Nadine Sundquist (AdWords API Team)

unread,
Dec 13, 2016, 5:17:28 PM12/13/16
to AdWords API Forum
Hi Burak,

That's some great debugging skills! I'm glad you were able to figure out the root of the issue. Please file an issue with the PHP client library if there is not already an issue on this.

Thanks,
Nadine, AdWords API Team
Reply all
Reply to author
Forward
0 new messages