Bad request because of the modal window

33 views
Skip to first unread message

Meurch

unread,
Apr 4, 2022, 11:28:13 AM4/4/22
to Google Ads API and AdWords API Forum
So I already made a complete authorization on my site using Google auth.
But I need now to add a modal window with a "Terms of usage". So I made it the next way:
 after the clicking on the agree button, we are going to the script:

    $params = array(
        'client_secret' => '6AQdBsdfefwghu263-TkR',
        'redirect_uri'  => 'https://mysite.ru/_scripts/login_google.php',
        'grant_type'    => 'authorization_code',
        'code'          => $_GET['code']
    );  

    $code_to_auth = $_GET['code'];
    print_r($code_to_auth);
   
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    $data = curl_exec($ch);
    curl_close($ch);    
 
    $data = json_decode($data, true);
    print_r($data);

I got an error , but the same code is working well for the login without the modal window. Maybe there are some parameters I forgot about? So I already choose my google account and I just used
document.location.href
 to define get parameters for the ajax which is going to the php script mentioned above.

[error] => invalid_grant [error_description] => Bad Request

I changed client id, secret and site domain, in my code these parameters are correct.

Google Ads API Forum Advisor

unread,
Apr 4, 2022, 3:13:08 PM4/4/22
to meurc...@gmail.com, adwor...@googlegroups.com
Hello,

Thanks for reaching out. I understand that you are receiving an invalid_grant error. This error can usually be resolved by regenerating the refresh token. Can you please try this, and if you are still receiving the invalid_grant error, reply to us privately with the complete request and response?

Thanks,
Matt
Google Ads API Team

Google Logo
Matt
Google Ads API Team
 


ref:_00D1U1174p._5004Q2ZJYlC:ref

Meurch

unread,
Apr 5, 2022, 5:55:19 AM4/5/22
to Google Ads API and AdWords API Forum
Can you say more about the refresh token? So firstly I should do this to choose my account:
$params = array(
    'redirect_uri'  => 'https://sdf.ru/_scripts/login_google.php',
    'response_type' => 'code',
    'state'         => '123'
     );
                                     
 $url = 'https://accounts.google.com/o/oauth2/auth?' . urldecode(http_build_query($params));

After that, as I understand maybe incorrectly, I need only the $auth_code to get the $data:

$params = array(
        'client_id'     => '105310604kaaff65eo0kaop1v5.apps.googleusercontent.com',
        'client_secret' => '6AQdrSjG2u263-TkR',
        'redirect_uri'  => 'https://sdf.ru/_scripts/login_google.php',
        'grant_type'    => 'authorization_code',
        'code'          => $auth_code
    );  
   
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    $data = curl_exec($ch);
    curl_close($ch);    


So I got an error that data is incorrect.
I saw via the debug $auth_code is the same as I got before the modal Window step.

So, maybe there are some additional parameters I forgot, or what should I check next?

понедельник, 4 апреля 2022 г. в 22:13:08 UTC+3, adsapi:

Google Ads API Forum Advisor

unread,
Apr 5, 2022, 9:30:53 AM4/5/22
to meurc...@gmail.com, adwor...@googlegroups.com
Hello,

If you are using the supported PHP client library, you should use one of the authentication code samples depending on which auth flow you are using (web/desktop). You can find the authentication code samples found for each flow on the following pages: You can follow the steps given to regenerate your refresh token, which should resolve the invalid_grant error. After generating the token, please copy it to your .INI file. 

Regards,
Reply all
Reply to author
Forward
0 new messages