63

有没有一种方法可以强制出现谷歌账户选择器,即使用户只使用一个账户登录。

我已经尝试重定向到这个 URL:

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

它似乎有效,但我不知道是否还有其他可能失败的条件。

在此处输入图像描述

4

5 回答 5

103

OAuth2 授权 URL 支持以下参数:

prompt

目前它可以有值noneselect_accountconsent

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

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

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

select_account可以与 结合consent,如:

prompt=select_account consent

于 2013-01-18T06:24:57.870 回答
13

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

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

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

于 2016-08-22T13:10:26.633 回答
10

有些人最终可能会在这里寻找有关如何在 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");
  });
于 2018-03-29T14:56:16.630 回答
3

如果您使用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();
                });
            });
        });
于 2019-03-29T06:19:37.957 回答
2

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

$client = new Google_Client();
$client->setApprovalPrompt("force");
$client->createAuthUrl();
于 2019-06-18T09:21:10.980 回答