【问题标题】:Force google account chooser强制谷歌帐户选择器
【发布时间】:2013-01-01 07:16:53
【问题描述】:

有没有一种方法可以强制显示 google 帐户选择器,即使用户仅使用一个帐户登录也是如此。

我已尝试重定向到此 URL:

https://accounts.google.com/AccountChooser?service=lso&continue=[authorizeurl]

它似乎工作,但我不知道是否有任何其他条件可能会失败。

【问题讨论】:

    标签: google-oauth


    【解决方案1】:

    OAuth2 授权 URL 支持以下参数:

    prompt

    目前它可以有值noneselect_accountconsent

    • none:将导致 Google 不显示任何 UI,因此如果用户需要登录则失败,或者在多次登录的情况下选择一个帐户,或者如果首次批准则同意。它可以在不可见的 i-frame 中运行,以便在您决定(例如,呈现授权按钮)之前从先前授权的用户那里获取令牌。

    • 同意:即使用户之前已授权您的应用程序,也会强制显示批准页面。在一些特殊情况下可能很有用,例如,如果您丢失了用户的 refresh_token,因为 Google 仅在明确同意操作时才会发出 refresh_tokens。

    • select_account:将显示帐户选择器,即使只有一个登录用户,正如您所要求的那样。

    select_account 可以与consent 组合,如:

    prompt=select_account consent

    【讨论】:

    • 'approval_prompt=force' 和 'prompt=consent' 一样吗?谢谢
    • 是的,但与 prompt=consent 不同,它不能与“select_account”选项结合使用。如果现在编写新代码,请改用“提示”。
    • 有没有办法强制使用 gmail 帐户(如 hd=gmail.com)登录?
    • @woloski,是的,hd=default 应该限制为 gmail 帐户
    • prompt=select_account+consent 不起作用,您需要使用 prompt=select_account 同意 ------ 文档:developers.google.com/accounts/docs/OpenIDConnect
    【解决方案2】:

    另外,您可以在 HTML 标记中添加“提示”参数作为 data-prompt="select_account":

    <div class="g-signin2" data-onsuccess="onSignIn" data-prompt="select_account"> 
    

    它每次都会强制选择帐户,即使您只使用一个帐户登录

    【讨论】:

    • 不适合我,&lt;div class="g-signin2" data-scope="profile email" data-width="298" data-onsuccess="onSignIn" data-prompt="select_account" &gt;&lt;/div&gt; 但每次仍然调用 onSignIn
    • @BenjaminPoignant 您的问题(页面加载时立即调用成功处理程序)与此问题所涉及的问题略有不同(尽管您的困惑是可以理解的)。正如stackoverflow.com/a/15503280/1709587 所说,您需要使用gapi.auth2.getAuthInstance().signOut(); 将用户从您的应用程序中注销。 这个问题是关于如何做到这一点,以确保当用户再次单击登录按钮时,他们有机会选择要登录的 Google 帐户,而不是立即使用他们当前的 Google 帐户只要点击登录按钮。
    【解决方案3】:

    有些人最终可能会在这里寻找有关如何在 Microsoft.AspNetCore.Authentication 中执行此操作的答案。

    我们可以通过 Startup.ConfigureServices 方法中的以下代码来完成它:

    services.AddAuthentication()
      .AddGoogle(options =>
      {
          options.ClientId = configHelper.GoogleOAuthClientID;
          options.ClientSecret = configHelper.GoogleOAuthSecret;
          options.CallbackPath = "/signin-google";
          options.AuthorizationEndpoint = string.Concat(options.AuthorizationEndpoint, "?prompt=select_account");
      });
    

    【讨论】:

      【解决方案4】:

      如果您使用的是gapi,而不仅仅是添加prompt: 'select_account'
      示例:

      gapi.load('auth2', function () {
                  gapi.auth2.init({
                      client_id: "client_id.apps.googleusercontent.com",
                      scope: "profile email", // this isn't required
                      ux_mode: 'redirect',
                      redirect_uri: 'https://www.example.com',
                      prompt: 'select_account'
                  }).then(function (auth2) {
                      console.log("signed in: " + auth2.isSignedIn.get());
                      x = auth2.isSignedIn.get();
                      auth2.isSignedIn.listen(onSignIn);
                      var button = document.querySelector('#signInButton');
                      button.addEventListener('click', function () {
                          auth2.signIn();
                      });
                  });
              });
      

      【讨论】:

        【解决方案5】:

        对于 google api php 客户端 (https://github.com/google/google-api-php-client),您可以按以下方式进行操作:

        $client = new Google_Client();
        $client->setApprovalPrompt("force");
        $client->createAuthUrl();
        

        【讨论】:

          猜你喜欢
          • 2014-06-19
          • 2017-07-27
          • 2019-10-28
          • 2013-09-24
          • 2020-01-20
          • 2016-10-09
          • 2017-06-17
          • 1970-01-01
          • 2016-12-26
          相关资源
          最近更新 更多