Skip to content

Instantly share code, notes, and snippets.

@aknishiumi
Created January 27, 2012 02:16
Show Gist options
  • Save aknishiumi/1686561 to your computer and use it in GitHub Desktop.
Save aknishiumi/1686561 to your computer and use it in GitHub Desktop.
OpenSocial: OAuth2 Gadget sample
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="OpenSocialGadget OAuth2 sample">
<OAuth2>
<Service name="[service_name]">
<Authorization url="https://.../authorize"/>
<Token url="https://.../oauth2/token"/>
</Service>
</OAuth2>
<Require feature="oauthpopup" />
</ModulePrefs>
<Content type="html">
<![CDATA[
<div id="approval" style="display: none">
<a href="#" id="personalize">Personalize this gadget</a>
</div>
<div id="waiting" style="display: none">
Please click
<a href="#" id="approvaldone">I've approved access</a>
once you've approved access to your data.
</div>
<div id="main" class="main" style="display: none"></div>
<script type="text/javascript">
function showOneSection(toshow) {
var sections = [ 'main', 'approval', 'waiting' ];
for (var i=0; i < sections.length; ++i) {
var s = sections[i];
var el = _gel(s);
if (s === toshow) {
el.style.display = "block";
} else {
el.style.display = "none";
}
}
}
function loadContents(){
var url = "http://www...";
var params = {};
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.OAUTH2;
params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
params[gadgets.io.RequestParameters.OAUTH_SERVICE_NAME] = "[service_name]";
var callback = function (response) {
if (response.oauthApprovalUrl) {
var onOpen = function() {
showOneSection('waiting');
};
var onClose = function() {
buildContainer();
};
var popup = new gadgets.oauth.Popup(response.oauthApprovalUrl, null, onOpen, onClose);
_gel('personalize').onclick = popup.createOpenerOnClick();
_gel('approvaldone').onclick = popup.createApprovedOnClick();
showOneSection('approval');
} else if (response.data) {
showOneSection('main');
// TODO:display contents
}
};
gadgets.io.makeRequest(url, callback, params);
}
gadgets.util.registerOnLoadHandler(loadContents);
</script>
]]>
</Content>
</Module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment