Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mataspetrikas/236842 to your computer and use it in GitHub Desktop.
Save mataspetrikas/236842 to your computer and use it in GitHub Desktop.
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
/**
Lazy-load Google gdata libraries and check for the user authorization
@gscope - the google service scope you want to be authorized to, e.g. "http://www.blogger.com/feeds"
@callback - a function to be called after succesfull authorization
*/
getGdataAndAuthorize = function(gscope, callback) {
var checkInterval,
time = 100,
verify = function() {
var loggedIn = !!google.accounts.user.checkLogin(gscope);
if (loggedIn) {
callback();
}else{
google.accounts.user.login(gscope);
}
},
poll = function() {
checkInterval = setTimeout(function() {
if(location.hash.length){
// the auth token is still in the hash, try one more time
poll();
}else{
// the token was consumed, let's check if auth was succesfull
verify();
}
}, time);
time = time * 2;
},
onGDataLoaded = function() {
// this needs to be called so gdata consumes the token
google.gdata.onLoad();
// in case we're returning from auth page and we have the token in location hash
if(location.hash.length){
// start polling for token processing
poll();
}else{
// if it's a simple load, verify if we're logged in
verify();
}
};
// load the gdata libs
google.load("gdata", "1.x", {
callback : onGDataLoaded
});
};
getGdataAndAuthorize("http://www.blogger.com/feeds", function() {
alert('gdata loaded and authorized\'re logged in!');
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment