2

I'm working on Yii2, using google/apiclient. I keep getting "Error: redirect_uri_mismatch"

Nothing seems to work, any insights?

My code is nothing special:

$session = Yii::$app->session;

    $gClient = new Google_Client();
    $pathToSecret = Yii::getAlias('@app/auth/gCalendar_ClientSecret.json');
    $gClient->setAuthConfig($pathToSecret);
    $gClient->addScope(Google_Service_Calendar::CALENDAR_EVENTS_READONLY);
    $gClient->setLoginHint('my_mail@not.relevant');
    if ($session->has('oauth_access_token')) {
        $gClient->setAccessToken($session->get('oauth_calendar_access_token'));
        //do something else
    } else {
        $redirectUri = Url::toRoute('/calendar/oauth-response', 'http');
        $gClient->setRedirectUri($redirectUri);
        return $gClient->createAuthUrl();
    }

Based on the error, the request seems fine:

The redirect URI in the request, http://localhost/ascoSL/public_html/sl/index.php?r=calendar%2Foauth- response, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/XXXXX

See a capture of my google config

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Edo
  • 33
  • 4

2 Answers2

2

You need to register your API endpoint in the google API console:

Please note that the exact (absolute) route you're using needs to be registered

This has already been answered here and im sure you can find your answer here too: Google OAuth 2 authorization - Error: redirect_uri_mismatch

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Sweet Chilly Philly
  • 3,014
  • 2
  • 27
  • 37
  • I've done that, I'm showing the config in a picture link. Here it is again for you good sir: [link](https://i.stack.imgur.com/xnaqA.png) – Edo Oct 08 '19 at 21:32
  • Your redirect url you are sending to the server, doesnt look like it will match what you have in the google console: These lines in your Else statement: $redirectUri = Url::toRoute('/calendar/oauth-response', 'http'); $gClient->setRedirectUri($redirectUri); – Sweet Chilly Philly Oct 08 '19 at 21:34
  • Also add http://localhost:8080, and also make sure your server IS actaully running on port 8080 – Sweet Chilly Philly Oct 08 '19 at 21:36
  • @SweetChillyPhilly you dont need to anwser a question with a link to the duplicate you can just flag it as duplicate if you dont have permissions to flag duplicates just leave it as a comment and someone with the permissions to create the flag will. – Linda Lawton - DaImTo Oct 09 '19 at 06:28
  • @dalmTo thanks for that, really helpful advice and ill use it in the future when doing triage. Though; as it turns out Edo had read that article and it hadn't helped, so Im happy i did end up answering and helping him through his issue :) – Sweet Chilly Philly Oct 09 '19 at 22:30
0

Ok so turns out the EXACT absolute route has to be registered, I assumed sub-directories would be allowed. Once the full URI was registered, redirect worked fine. Thanks SweetChillyPhilly for the link.

Edo
  • 33
  • 4
  • Hey bro, sorry but you don't add your own answer which is an exact copy of my answer. You upvote my answer then I would go ahead and consolidate the comments into the answer itself. – Sweet Chilly Philly Oct 08 '19 at 21:42
  • 1
    Yeah, i was writing this during your answer, didn´t notice. Cheers – Edo Oct 09 '19 at 15:48