【问题标题】:yii2: problems using google/apiclient for oAuth and token authenticationyii2:使用 google/apiclient 进行 oAuth 和令牌身份验证时出现问题
【发布时间】:2019-02-26 12:58:53
【问题描述】:

我的项目是在 Yii2 上构建的,并使用google/apiclient 来登录 Web 界面。还有一个 android 应用程序连接到 API 并使用对 Google 令牌的不记名身份验证(我相信它被拉入 firebase/jwt)。从 2018 年初到 2018 年 9 月 10 日开始的那一周,这一直运行良好。我的系统中没有更改任何代码。

从那时起,尝试登录 Web 界面 (oAuth) 会给出

yii\authclient\InvalidResponseException:请求失败,代码:400,消息:{ “错误”:“redirect_uri_mismatch”, "error_description" : "错误请求"

该网站已正确列在 Google 开发者控制台中(我也没有更改任何内容),因此不应出现 redirect_uri_mismatch

升级google/apiclient 允许我登录到网络界面,但会破坏应用程序的令牌身份验证,给出:

您的请求是使用无效凭据提出的

如果需要,我可以提供完整的堆栈跟踪,但是,我希望其他人也遇到过同样的情况,并且可以为我指明正确的方向。在实时系统中使用 firebase/jwt v4 允许令牌身份验证运行,但在测试中使用升级后的 google/apiclient 时使用 v4 会导致身份验证失败,并出现与上述相同的凭据错误。

有人可以提供任何指导吗?

【问题讨论】:

  • 这描述了一个类似的问题,升级解决了使用 oAuth 登录 Web 界面的问题,但不幸的是这样做会破坏令牌身份验证(与原始帖子相同的问题)。

标签: php google-api yii2 google-authentication


【解决方案1】:

要在首次登录时获取刷新令牌,您需要在“authUrl”中设置“?access_type=offline”。然后将其保存在某处,数据库,文件等中。

如果您没有数据库,或者您可以在每次登录时提示访问权限,您可以将“&approval_prompt=force”添加到相同的 URL。这将强制 Google 在您每次登录时向您发送刷新令牌,但也会强制每次都显示访问权限提示。

class MyGoogleClient extends yii\authclient\clients\Google
{

        /**
         * Set to true if you want to get refresh token always on login
         * @var type
         */
        public $enforceRefreshToken = false;

        /**
         * {@inheritdoc}
         */
        public function init()
        {
            parent::init();

            if (is_array($this->scope)) {
                $this->scope = implode(' ', $this->scope);
            }

            $additionalParams = [];
            if ($this->autoRefreshAccessToken) {
                $additionalParams['access_type'] = 'offline';
            }
            if ($this->enforceRefreshToken) {
                $additionalParams['approval_prompt'] = 'force';
            }
            if (!empty($additionalParams)) {
                $this->authUrl = $this->composeUrl($this->authUrl, $additionalParams);
            }
        }

        /**
         * {@inheritdoc}
         * @return string return URL.
         */
        protected function defaultReturnUrl()
        {
            return Yii::$app->getUrlManager()->createAbsoluteUrl([Yii::$app->controller->getRoute(), 'authclient' => 'google']);
        }

    }

【讨论】:

    猜你喜欢
    • 2021-06-09
    • 2017-10-05
    • 2019-11-29
    • 2016-05-19
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多