所以我正在努力将谷歌登录/注销到我的网站。我能够通过后端登录和注销用户。问题是,如果您登录然后注销,然后尝试再次登录,Google 不会询问您要使用的帐户。它会自动使用您以前的帐户。
如何注销用户,以便在他们重新登录时提示他们选择一个帐户?
如果有人好奇,这是我的代码
<?php session_start();
if(isset($_GET['logout']) && $_GET['logout'] == 1){
//logout
unset($_SESSION["access_token"]);
// setcookie('G_AUTHUSER_H', 'derp', strtotime( '1 second' ));
echo 'logout ran';
}
//https://github.com/google/google-api-php-client/tree/v1-master
include ($_SERVER[DOCUMENT_ROOT]."/google-api/autoload.php");
// ########## Google Settings.Client ID, Client Secret from https://console.developers.google.com #############
$client_id = 'xxxx';
$client_secret = 'xxxx';
$redirect_uri = 'http://localhost:8888/test2.php';
// ###################################################################
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("email");
$client->addScope("profile");
$service = new Google_Service_Oauth2($client);
//If code is empty, redirect user to google authentication page for code.
//Code is required to aquire Access Token from google
//Once we have access token, assign token to session variable
//and we can redirect user back to page and login.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
exit;
}
//if we have access_token continue, or else get login URL for user
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
$authUrl = $client->createAuthUrl();
}
//Display user info or display login url as per the info we have.
echo '<div style="margin:20px">';
if (isset($authUrl)){
//show login url
echo '<div align="center">';
echo '<h3>Login with Google -- Demo</h3>';
echo '<div>Please click login button to connect to Google.</div>';
echo '<a class="login" href="' . $authUrl . '">Login</a>';
echo '</div>';
} else {
$user = $service->userinfo->get(); //get user info
$user_count = FALSE;
if($user_count)
{
echo 'Welcome back '.$user->name.'! [<a href="'.$redirect_uri.'?logout=1">Log Out</a>]';
}
else //else greeting text "Thanks for registering"
{
echo 'Hi '.$user->name.', Thanks for Registering! [<a href="'.$redirect_uri.'?logout=1">Log Out</a>]';
}
// echo $user->email;
echo '<pre>';
var_dump($user);
echo '</pre>';
}
echo '<br><br><br>Cookies';
var_dump($_COOKIE);
echo '<br><br><br>Sessions';
var_dump($_SESSION);
echo '</div>';
echo '<p>working</p>';
?>
如果我的问题不清楚或令人困惑,请说出来。
您应该将$client->setApprovalPrompt('select_account consent')添加到新的Google_Client()中。
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("email");
$client->addScope("profile");
$client->setApprovalPrompt('select_account consent');