使用 google 帐户静默刷新令牌时出错

Error in silent refresh of token with google account

在 angular CLI Web 应用程序中,我需要使用 google 个帐户对用户进行身份验证。

我正在使用 angular_oauth_oidc 库来管理所有与 oauth 相关的任务。我已将 OAutService 配置为自动静默刷新。 我可以在 fiddler 中看到该服务正在发出令牌刷新请求,但响应是错误的。

这是对 accounts.google.com 的请求(不含敏感信息):

GET /o/oauth2/v2/auth?response_type=id_token%20token&client_id=[MY_CLIENT_ID]&state=[MY_STATE]&redirect_uri=http%3A%2F%2Flocalhost%3A4200%2Fassets%2Fsilent-refresh.html&scope=openid%20profile%20email&nonce=[MY_NONCE]&prompt=none&id_token_hint=[MY TOKEN] HTTP/1.1

这是回复:

HTTP/1.1 302 Found
Content-Type: application/binary
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Thu, 17 Jan 2019 15:49:21 GMT
Location: http://localhost:4200/assets/silent-refresh.html#state=[STATE]&error_subtype=access_denied&error=interaction_required

根据库文档和 oidc 标准,请求具有所有必需的参数,但我无法使其工作。

是否有人成功地使用 google 个帐户进行自动静默令牌刷新?

谢谢

你有这个错误:

  • error_subtype=access_denied
  • error=interaction_required

如果您将其包含在您的客户端应用程序中,您还应该在控制台上看到它:

import { OAuthErrorEvent } from 'angular-oauth2-oidc';
// ...
// Wherever you set up the library, do this:
this.authService.events.subscribe(e => (e instanceof OAuthErrorEvent) ? console.error(e) : console.warn(e));

基本上,错误意味着您的 IDS 拒绝向 Angular 应用程序发送令牌,直到用户与 IDS 进行某种交互。通常这意味着(重新)输入您的用户名 and/or 密码,或给予某种同意。

在客户端,当出现这种情况时,你应该调用initImplicitFlow()。在 my sample repository 中,我按照以下思路进行操作:

return this.oauthService.loadDiscoveryDocument()
  .then(() => this.oauthService.tryLogin())
  .then(() => {
    if (this.oauthService.hasValidAccessToken()) {
      return Promise.resolve();
    }

    // 2. SILENT LOGIN:
    return this.oauthService.silentRefresh()
      .then(() => Promise.resolve())
      .catch(result => {
        // Subset of situations from https://openid.net/specs/openid-connect-core-1_0.html#AuthError
        // Only the ones where it's reasonably sure that sending the
        // user to the IdServer will help.
        const errorResponsesRequiringUserInteraction = [
          'interaction_required',
          'login_required',
          'account_selection_required',
          'consent_required',
        ];

        if (result
          && result.reason
          && errorResponsesRequiringUserInteraction.indexOf(result.reason.error) >= 0) {

          // Enable this to ALWAYS force a user to login.
          this.oauthService.initImplicitFlow();

          // Should be superfluous, user should be redirected to IDS already...
          return Promise.reject();
        }

        return Promise.reject(result);
      });
  });