Google用OAUTH2给第三方鉴权
这里是一个html+javascript例子:
首先用它得到access_token:
https://accounts.google.com/o/oauth2/auth?client_id=133501856298.apps.googleusercontent.com&redirect_uri=http://www.hanborq.com/static/test_google_oauth.html&scope=https://www.google.com/m8/feeds/&response_type=token
再用它得到用户名字和gmail:
https://www.google.com/m8/feeds/contacts/default/thin?oauth_token=”+access_token+”&max-results=0″
这里可以进行测试:
http://www.hanborq.com/static/test_google_oauth.html
源文件如下(test_google_oauth.html):
<DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html;charset=utf-8″ />
<title>测试google OAUTH给第三方鉴权和授权</title>
<link rel=”shortcut icon” href=”http://adaishu.blog.163.com/blog/Hanborq-logo.ico” />
<link rel=”icon” type=”image/gif” href=”http://adaishu.blog.163.com/blog/Hanborq-logo-199X57-web.gif” />
<style type=”text/css”>
<!–
body{background-color:#FFF; background-image:url(images/base_bg.jpg); background-repeat:repeat-x; margin:0px; padding:0px;
line-height:200%;padding-top:10px;}
–>
</style>
<script type=”text/javascript”>
function GetUrlParms(){
var args=new Object();
var query=location.href;//获取查询串
var pos=query.indexOf(‘#’); //第二次从google重定向后返回的格式: http://xxx.xxx/yyy.yy#access_token=zzzz&expires_in=wwww
if(pos==-1)
return;
var query=query.substring(pos+1);
var pairs=query.split(“&”);//在逗号处断开
for(var i=0; i<pairs.length; i++)
{
var pos=pairs[i].indexOf(‘=’);//查找name=value
if(pos==-1) continue;//如果没有找到就跳过
var argname=pairs[i].substring(0,pos);//提取name
var value=pairs[i].substring(pos+1);//提取value
args[argname]=unescape(value);//存为属性
}
return args;
}
function GetName(rsptext){
var pos=rsptext.indexOf(“<name>”); //google返回格式: <name>阿呆</name>
if(pos==-1)
return;
var posend=rsptext.indexOf(“</name>”);
if(posend==-1)
return;
var name=rsptext.substring(pos+6,posend);
return name;
}
function GetUserId(rsptext){
var pos=rsptext.indexOf(“<id>”); //google返回格式: <id>adaishu@gmail.com</id>
if(pos==-1)
return;
var posend=rsptext.indexOf(“</id>”);
if(posend==-1)
return;
var userId=rsptext.substring(pos+4,posend);
return userId;
}
window.onload=function(){
var args = new Object();
args = GetUrlParms();
if(args==null) //第一次进来args为空
return;
if(args[“access_token”]==null) //第二次进来是从google重定向过来的,带有参数access_token。
return;
document.getElementById(‘datas’).innerHTML = “等待。。。”;
var access_token = args[“access_token”];
//alert(access_token);
try {
xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!=’undefined’) {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp=false;
}
}
if (!xmlhttp && window.createRequest) {
try {
xmlhttp = window.createRequest();
} catch (e) {
xmlhttp=false;
}
}
if(!xmlhttp)
return;
xmlhttp.open(“GET”, “https://www.google.com/m8/feeds/contacts/default/thin?oauth_token=”+access_token+”&max-results=0”, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status!=200)
alert(“服务器返回错误,状态是:”+xmlhttp.status +”;内容为:”+xmlhttp.responseText);
//alert(xmlhttp.responseText);
var userId = GetUserId(xmlhttp.responseText);
var name = GetName(xmlhttp.responseText);
//alert(“名字:”+name+”,GMail:”+userId);
document.getElementById(‘datas’).innerHTML = “名字:”+name+”,GMail:”+userId;
}
}
xmlhttp.send(null);
}
</script>
</head>
<body>
<a href=”http://www.hanborq.com/”><img style=”” src=”http://adaishu.blog.163.com/blog/Hanborq-logo-199X57-web.gif” /></a>
<p></p>
<div style=”text-align:center;”>
<h1>测试google OAUTH给第三方鉴权和授权</h1>
</div>
<div style=”text-align:center;”>
<p>firefox对于XMLHttpRequest的https请求,不能正确得到结果,所以要用IE,把http://www.google.com、https://www.google.com和http://www.hanborq.com加入IE信任站点,然后打开跨域访问,否则提示没有权限。</p>
</div>
<div style=”text-align:center;”>
<a href=”https://accounts.google.com/o/oauth2/auth?client_id=133501856298.apps.googleusercontent.com&redirect_uri=http://www.hanborq.com/static/test_google_oauth.html&scope=https://www.google.com/m8/feeds/&response_type=token”>请求google鉴权</a>
</div>
<div id=”datas”></div>
</body>
</html>