强制谷歌帐户选择器

问题描述 投票:50回答:5

是否有一种方法可以强制Google帐户选择器出现,即使用户只使用一个帐户登录也是如此。

我试过重定向到这个URL:

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

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

google-oauth
5个回答
87
投票

OAuth2授权URL支持以下参数:

prompt

目前它可以有值noneselect_accountconsent

  • none:将导致Google不显示任何UI,因此如果用户需要登录则失败,或者在多次登录时选择帐户,或者在首次批准时同意。它可以在不可见的i帧中运行,以便在您决定(例如)呈现授权按钮之前从先前授权的用户获取令牌。
  • 同意:即使用户之前已经授权您的申请,也会强制显示批准页面。在一些极端情况下可能很有用,例如,如果您丢失了用户的refresh_token,因为Google仅在明确同意操作时发出refresh_tokens。
  • select_account:将导致帐户选择器显示,即使有一个登录用户,就像你问的那样。

select_account可以与consent结合使用,如:

prompt=select_account consent


8
投票

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

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

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


6
投票

有些人可能会在这里找到关于如何在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");
  });

0
投票

如果你使用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();
                });
            });
        });

0
投票

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

$client = new Google_Client();
$client->setApprovalPrompt("force");
$client->createAuthUrl();
© www.soinside.com 2019 - 2024. All rights reserved.