即使用户只使用一个帐户登录,我是否可以强制显示 Google帐户选择器。
我尝试过重定向到这个网址:
https://accounts.google.com/AccountChooser?service=lso&continue=[authorizeurl]
它似乎有效,但我不知道是否还有其他条件可能会失败。
答案 0 :(得分:85)
OAuth2授权网址支持以下参数:
prompt
目前,它可以包含值none
,select_account
和consent
。
none:将导致Google不显示任何用户界面,因此如果用户需要登录则会失败,或者在多次登录时选择一个帐户,或者在首次批准时同意。例如,在您决定呈现授权按钮之前,它可以在不可见的i帧中运行以从先前授权的用户获取令牌。
同意:即使用户之前已经授权您的申请,也会强制显示批准页面。可能在一些极端情况下很有用,例如,如果您丢失了用户的refresh_token,因为Google仅在明确同意操作时发出refresh_tokens。
select_account:即使有一个登录用户,也会显示帐号选择器,就像你问的那样。
select_account
可与consent
结合使用,如:
prompt=select_account consent
答案 1 :(得分:8)
此外,您可以在HTML标记中添加“prompt”参数作为data-prompt =“select_account”:
<div class="g-signin2" data-onsuccess="onSignIn" data-prompt="select_account">
并且每次都会强制进行帐户选择,即使您只使用一个帐户登录
答案 2 :(得分:5)
有些人可能会在这里找到关于如何在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");
});
答案 3 :(得分: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();
});
});
});
答案 4 :(得分:0)
对于 google api php客户端(https://github.com/google/google-api-php-client),您可以按照以下步骤进行操作:
$client = new Google_Client();
$client->setApprovalPrompt("force");
$client->createAuthUrl();