Skip to content

Instantly share code, notes, and snippets.

@kmod-midori
Created April 6, 2013 13:25
Show Gist options
  • Save kmod-midori/5326112 to your computer and use it in GitHub Desktop.
Save kmod-midori/5326112 to your computer and use it in GitHub Desktop.
chrome.cookies.onChanged.addListener(function(ci){
if(ci.cookie.name=="oauth_code"&&ci.cookie.domain=="accounts.google.com"&&ci.cookie.path=="/o/oauth2/programmatic_auth"&&ci.removed==false){
chrome.extension.sendMessage(null,{oauth:ci.cookie.value});
}
});
sr = function(obj) {
var str = [];
for(var p in obj){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]).replace('*','%2A'));
}
return str.join("&");
}
login_error = function(){
$('.bar').parent().addClass('progress-danger');
$('#proc').text($('#proc').text()+' 失败!');
}
$(function(){
client_secret='q-tA1vMJhkoeoucVH7NfvryX';
client_id='545553600905.apps.googleusercontent.com';
$('#submit').click(function(e){
chrome.runtime.getBackgroundPage(function(){});
chrome.tabs.create({
"url":'https://accounts.google.com/o/oauth2/programmatic_auth?client_id=545553600905.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthLogin%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email'
},function(tab){
chrome.extension.onMessage.addListener(function(message, sender, sendResponse) {
if(typeof message.oauth =='undefined'){
return;
}
chrome.tabs.remove(tab.id);
$("#procModal").modal();
$('#proc').text('正在验证 - 第1步');
$.ajax({
url:'https://accounts.google.com/o/oauth2/token',
processData:false,
type:'POST',
dataType:'json',
error:login_error,
data:sr({
client_id:client_id,
client_secret:client_secret,
code:message.oauth,
grant_type:"authorization_code",
scope:'https://www.google.com/accounts/OAuthLogin https://www.googleapis.com/auth/userinfo.email'
}),
success:function(data,stat){
if(typeof data.access_token != 'undefined'){
$('.bar').css('width','20%');
$('#proc').text('正在验证 - 第2步');
var refresh_token=data.refresh_token;
var auth=data.token_type+' '+data.access_token;
$.ajax({
url:"https://www.googleapis.com/oauth2/v1/userinfo",
headers:{'Authorization':auth},
type:'GET',
dataType:'json',
error:login_error,
success:function(data,stat){
if(typeof data.id !='undefined'){
userinfo=data;
$('.bar').css('width','40%');
$('#proc').text('正在验证 - 第3步');
$.ajax({
url:'https://accounts.google.com/o/oauth2/token',
processData:false,
type:'POST',
dataType:'json',
error:login_error,
data:sr({
client_id:client_id,
client_secret:client_secret,
grant_type:'refresh_token',
refresh_token:refresh_token
}),
success:function(data,stat){
if(typeof data.access_token != 'undefined'){
$('.bar').css('width','60%');
$('#proc').text('正在验证 - 第4步');
auth=data.token_type+' '+data.access_token;
$.ajax({
url:'https://www.googleapis.com/oauth2/v2/IssueToken',
processData:false,
headers:{'Authorization':auth},
type:'POST',
dataType:'json',
error:login_error,
data:sr({
app_id:'com.google.GooglePlus.*',
client_id:client_id,
client_secret:client_secret,
response_type:'code',
hl:'zh-Hans',
scope:"https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.stream.read https://www.googleapis.com/auth/plus.stream.write https://www.googleapis.com/auth/plus.circles.write https://www.googleapis.com/auth/plus.circles.read https://www.googleapis.com/auth/plus.photos.readwrite https://www.googleapis.com/auth/plus.native"
}),
success:function(data,stat){
if(typeof data.code !='undefined'){
$('.bar').css('width','80%');
$('#proc').text('正在验证 - 第5步');
$.ajax({
url:'https://accounts.google.com/o/oauth2/token',
processData:false,
type:'POST',
dataType:'json',
error:login_error,
data:sr({
client_id:client_id,
client_secret:client_secret,
code:data.code,
grant_type:"authorization_code",
redirect_uri:'urn:ietf:wg:oauth:2.0:oob'
}),
success:function(data,stat){
if(typeof data.access_token != 'undefined'){
$('.bar').css('width','100%').parent().addClass('progress-success');
$('#proc').text('验证成功,正在处理');
console.log(data);
}else{login_error();}
}
});
}else{login_error();}
}
});
}else{login_error();}
}
});
}else{login_error();}
}
});
}else{login_error();}
}
});
});
});
});
});
{
"manifest_version": 2,
"name": "Google Plus Plus Login Helper",
"version": "1.0",
"background": { "scripts": ["background.js"],"persistent": false},
"permissions": [
"cookies",
"*://*.google.com/",
"*://*.googleapis.com/",
"tabs",
"storage"
],
"options_page":"opt.html"
}
<html>
<head>
<title>Google++ Backup Helper</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="/bootstrap/css/bootstrap.css">
<script type="text/javascript" src="content.js"></script>
</head>
<body>
<div class="well">
<button id="submit" class="btn btn-large btn-primary">Google账号授权</button>
<span class="help-inline">使用OAuth方式登录,请确认您已登录Google+。</span>
</div>
<div class="modal hide fade" id="procModal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="ModalLabel">正在处理</h3>
</div>
<div class="modal-body">
<span class="help-block" id='proc'>正在授权,这可能需要一点时间。</span>
<div class="progress progress-striped active">
<div class="bar" style="width: 0%;"></div>
</div>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment