Google 已停止对 OpenID 的支持,并于 2015 年 7 月 22 日删除了此端点。
批准的解决方案/答案已过时。
请迁移到 OAuth。如果使用客户端(javascript)登录,google 的本教程附带了一个功能齐全的示例。
下面的代码是另一个功能齐全的示例(刚刚测试过),但它更进一步并与服务器端 PHP 脚本集成。请替换您自己的客户 ID(在您的google 开发者控制台中定义)和 YOURDOMAIN.COM(在 php 中)。
<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="1111111111111111111-11111111111111111111111111111111111.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
</script>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
userDataClear();
});
}
function disassociate() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.disconnect().then(function () {
console.log('User disconnected from association with app.');
userDataClear();
});
}
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId()); // Don't send this directly to your server!
// The ID token you need to pass to your backend:
var id_token = googleUser.getAuthResponse().id_token;
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://YOURDOMAIN.COM/twoStep02.php');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
console.log('NEW Signed in Google Client ID: ' + xhr.responseText);
};
xhr.send('idtoken=' + id_token);
console.log("ID Token: " + id_token);
userDataDisplay(profile);
}
function userDataDisplay(profile) {
document.getElementById("foto").innerHTML = '<img src="' + profile.getImageUrl() + '"></img>';
document.getElementById("email").innerHTML = profile.getEmail();
document.getElementById("nome").innerHTML = profile.getName();
}
function userDataClear() {
document.getElementById("foto").innerHTML = ' ';
document.getElementById("email").innerHTML = ' ';
document.getElementById("nome").innerHTML = ' ';
}
</script>
</head>
<body>
<div id="login-button" class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
<div><a href="/#" onclick="signOut();">Sign out</a></div>
<div><a href="/#" onclick="disassociate();">Disassociate App and Site (easily undone)</a></div>
<div id="foto"></div>
<div id="nome"></div>
<div id="email"></div>
</body>
</html>
这是 jquery/ajax 调用的 php 端(twoStep.php)
<?php
$idToken = $_POST["idtoken"];
$url = 'https://www.googleapis.com/oauth2/v3/tokeninfo?id_token='.$idToken;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$json = json_decode($response, true);
curl_close($ch);
$userEmail = $json["email"];
$clientId = $json["azp"];
print_r($json); // returns array console readable
?>
提示 4 新手:通过右键单击页面的任何元素并选择“检查元素”来读取控制台输出,然后切换到 CONSOLE 选项卡。