现在我们将 GoogleAPI 与 Google+ 一起使用
截至 2013 年 12 月,这是最新的网站;
https://developers.google.com/+/
然后用于 Web 登录
https://developers.google.com/+/web/signin/
选择登录流程
->客户端流程
->使用 JavaScript 启动登录流程(我相信这是最新的技术)
https://developers.google.com/+/web/signin/javascript-flow
使用 JavaScript 启动 Google+ 登录流程
您可以使用 gapi.auth.signIn() 启动 Google+ 登录流程
方法。这种方法为您提供了很大的灵活性来决定如何和
何时提示用户授权您的应用并登录。
https://developers.google.com/+/web/api/javascript#gapiauthsigninparameters
gapi.auth.signIn(参数)
启动客户端 Google+ 登录 OAuth 2.0 流程。如同
gapi.auth.authorize() 除了这个方法支持高级
Google+ 登录功能,包括无线安装 Android
应用。此方法是使用 Google+ 的 JavaScript 替代方法
登录按钮小部件。
https://developers.google.com/+/web/signin/javascript-flow
-
试一试页面使用 gapi.auth.signIn() 触发登录流程
https://google-developers.appspot.com/+/demos/signin_demo_render(源代码)
你会尝试这个,并为你自己,跟随
第 1 步:创建客户端 ID 和客户端密码
忽略以下步骤,
其实只需要clientID,替换上面Try it源码中的那个即可。
添加范围https://www.googleapis.com/auth/userinfo.email
var options = {
'callback': loginFinished,
'approvalprompt': 'force',
'clientid': 'YOURID.apps.googleusercontent.com',
'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',
'requestvisibleactions': 'http://schemas.google.com/CommentActivity http://schemas.google.com/ReviewActivity',
'cookiepolicy': 'single_host_origin'
};
添加
gapi.client.load('oauth2', 'v2', function()
{
gapi.client.oauth2.userinfo.get()
.execute(function(resp)
{
// Shows user email
console.log(resp.email);
});
});
以下是基于上述内容的完整简洁代码:
<html>
<head>
<title>Google+ Sign-in button demo: rendering with JavaScript</title>
<style type="text/css">
html, body { margin: 0; padding:0;}
#signin-button {
padding: 5px;
}
#oauth2-results pre { margin: 0; padding:0; width: 600px;}
.hide { display: none;}
.show { display: block;}
</style>
<script src="https://apis.google.com/js/client:platform.js" type="text/javascript"></script>
<script type="text/javascript">
var loginFinished = function(authResult)
{
if (authResult)
{
console.log(authResult);
}
gapi.client.load('oauth2', 'v2', function()
{
gapi.client.oauth2.userinfo.get()
.execute(function(resp)
{
// Shows user email
console.log(resp.email);
});
});
};
var options = {
'callback': loginFinished,
'approvalprompt': 'force',
'clientid': 'YOURID.apps.googleusercontent.com',
'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',
'requestvisibleactions': 'http://schemas.google.com/CommentActivity http://schemas.google.com/ReviewActivity',
'cookiepolicy': 'single_host_origin'
};
var renderBtn = function()
{
gapi.signin.render('renderMe', options);
}
</script>
</head>
<body onload ="renderBtn()">
<div id="renderMe"></div>
</body>
</html>
没有 HTML 输出,而是控制台。所以打开浏览器的开发者控制台工具来查看结果。