0

I am trying to run Google Drive API using Google Client V3 in PHP. I have credentials for my locahost site set up, have downloaded the JSON and load the client with those credentials and on my local site everything works fine.

When I move it to the live site and download the JSON for the the project we use on the live site I keep getting a redirect_uri_mismatch. I've checked the console for this project and the redirect uri's are set correctly. I have the following for redirect uri

https://www.awesomestories.com/Google/getClient
https://awesomestories.com/Google/getClient
https://www.awesomestories.com/Google/getClient/
https://awesomestories.com/Google/getClient

I realize this is over kill but I wanted to make sure every possible version was set to eliminate this as the problem. The Google prompt is happening and when the code is returned and I try to to load the code parameter into Google client:

$resp = $client->authenticate($_GET['code']);

I get the following response

array(3) {
  ["error"]=>
    string(21) "redirect_uri_mismatch"
  ["error_description"]=>
    string(11) "Bad Request"
  ["error_uri"]=>
    string(0) ""
}

The url that it is being redirected to is

https://www.awesomestories.com/Google/getClient

Which matches what is set in the console. Also we use this same project's credentials on the site already although we are just using the API key associated with this project not the Oauth2 credentials which GoogleClient is using. So for this project this is the first time we are using the Oauth2 credentials set up in the Google Console. I just can't figure out why the authentication is succeeding on the test site but not on the live site.

Another thing that is strange is that initialization of the client looks like this:

         $client = new Google_Client();
         $client->setAuthConfigFile(SYSTEM_PATH.'/shared/php/google-api-php-client/client-prod.json');
         $client->setAccessType('offline');
         $client->setApprovalPrompt("force");
         $client->setPrompt("consent");

and on my local site when the google prompt authorization screen comes up it asks to allow offline access, but on the live site it never asks to allow offline access.

If someone could point me to what is going wrong I would really appreciate it.

  • If you are receiving a **`redirect_uri_mismatch`** error, you might want to check your API console if you are using the correct OAuth 2.0 client IDs for the respective urls. A related SO question - [Google OAuth 2 authorization - Error: redirect_uri_mismatch](http://stackoverflow.com/a/11485644/5995040) that helps debug this kind of error. Also as a suggestion, provide a different in OAuth 2.0 client ID for live and testing sites. Hope this helps. – Mr.Rebot Nov 05 '16 at 15:30
  • Thanks Mr.Rebot. I checked the console and the OAuth credentials and they were right. I'm not sure why, but this fixed it for me: I removed all the redirect uris and re added them then ran my code and it worked. – Kirk Johnson Nov 06 '16 at 17:07

1 Answers1

0

So I don't know why this worked, but it did. I removed all the redirect uris from my console with the project associated with this, ran my code and at prompt screen got a redirect mismatch uri error as expected, I then re added the redirect uris, copying the one from the error message displayed on the prompt and then ran my code and the call succeeded. The strange thing is that the redirect I copied from the error message was the same that was in there before, because I copied the ones I deleted and compared them so I have no idea why this worked. Also before when I set the Google Client with the redirect uri the prompt screen went through fine without a hitch it was only when trying to load the authorization code sent back from Google prompt screen that the uri mismatch error would show up. I've ran Google prompts before where the redirect uri was set incorrectly on the client and this error appears in the prompt screen, so I'm not positive what was going on here, but it now is working.