【发布时间】:2013-01-01 07:16:53
【问题描述】:
有没有一种方法可以强制显示 google 帐户选择器,即使用户仅使用一个帐户登录也是如此。
我已尝试重定向到此 URL:
https://accounts.google.com/AccountChooser?service=lso&continue=[authorizeurl]
它似乎工作,但我不知道是否有任何其他条件可能会失败。
【问题讨论】:
标签: google-oauth
有没有一种方法可以强制显示 google 帐户选择器,即使用户仅使用一个帐户登录也是如此。
我已尝试重定向到此 URL:
https://accounts.google.com/AccountChooser?service=lso&continue=[authorizeurl]
它似乎工作,但我不知道是否有任何其他条件可能会失败。
【问题讨论】:
标签: google-oauth
OAuth2 授权 URL 支持以下参数:
prompt
目前它可以有值none、select_account和consent。
none:将导致 Google 不显示任何 UI,因此如果用户需要登录则失败,或者在多次登录的情况下选择一个帐户,或者如果首次批准则同意。它可以在不可见的 i-frame 中运行,以便在您决定(例如,呈现授权按钮)之前从先前授权的用户那里获取令牌。
同意:即使用户之前已授权您的应用程序,也会强制显示批准页面。在一些特殊情况下可能很有用,例如,如果您丢失了用户的 refresh_token,因为 Google 仅在明确同意操作时才会发出 refresh_tokens。
select_account:将显示帐户选择器,即使只有一个登录用户,正如您所要求的那样。
select_account 可以与consent 组合,如:
prompt=select_account consent
【讨论】:
另外,您可以在 HTML 标记中添加“提示”参数作为 data-prompt="select_account":
<div class="g-signin2" data-onsuccess="onSignIn" data-prompt="select_account">
它每次都会强制选择帐户,即使您只使用一个帐户登录
【讨论】:
<div class="g-signin2" data-scope="profile email" data-width="298" data-onsuccess="onSignIn" data-prompt="select_account" ></div> 但每次仍然调用 onSignIn
gapi.auth2.getAuthInstance().signOut(); 将用户从您的应用程序中注销。 这个问题是关于如何做到这一点,以确保当用户再次单击登录按钮时,他们有机会选择要登录的 Google 帐户,而不是立即使用他们当前的 Google 帐户只要点击登录按钮。
有些人最终可能会在这里寻找有关如何在 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");
});
【讨论】:
如果您使用的是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();
});
});
});
【讨论】:
对于 google api php 客户端 (https://github.com/google/google-api-php-client),您可以按以下方式进行操作:
$client = new Google_Client();
$client->setApprovalPrompt("force");
$client->createAuthUrl();
【讨论】: