Search code examples
phpgoogle-apigoogle-oauthgoogle-api-php-clientgoogle-developers-console

Does OAuth permit localhost for debugging?


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


Solution

  • 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