OAuth 是否允许 localhost 进行调试?

Does OAuth permit localhost for debugging?

我正在开发 Yii2,使用 google/apiclient。 我不断收到 "Error: redirect_uri_mismatch"

似乎没有任何效果,有任何见解吗?

我的代码没什么特别的:

$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();
    }

根据错误,请求似乎没问题:

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

您需要在 google API 控制台中注册您的 API 端点:

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

这已经在这里得到解答,我相信您也可以在这里找到您的答案:Google OAuth 2 authorization - Error: redirect_uri_mismatch

好的,事实证明必须注册确切的绝对路径,我假设子目录是允许的。 一旦注册了完整的 URI,重定向就可以正常工作。 感谢 SweetChillyPhilly link.