ok-js-sdk
Version:
OK JS SDK for frontend developers
79 lines (65 loc) • 2.91 kB
HTML
<html>
<head>
<meta charset="UTF-8">
<title>OKSDK - Advanced No-Refresh Hello World</title>
<script type="text/javascript" src="../oksdk.js"></script>
</head>
<body>
<div id="oauth_login">
<input id="btn_oauth" type="button" value="Login via OAUTH" onclick="startOAuthLogin();"/>
</div>
<div id="content"></div>
<script type="text/javascript">
var config = {
app_id: 0, // <-- insert APP ID here
app_key: '' // <-- insert APP PUBLIC KEY here
};
document.addEventListener('DOMContentLoaded', function () {
config.oauth = {};
config.oauth.url = location.origin + location.pathname; // setup callback url to our script
if (window.location.hash) {
// we are in popup and have OAUTH response data - send it back to the opener
var hash = OKSDK.Util.getRequestParameters(window.location.hash);
window.opener.postMessage(JSON.stringify({
'type': 'oauth',
'access_token': hash['access_token'],
'session_secret_key': hash['session_secret_key']
}), "*");
window.close();
return;
}
var args = OKSDK.Util.getRequestParameters(window.location.search);
if (args['run_oauth']) {
// we are in popup and requested to proceed with OAUTH login
OKSDK.init(config, function () {
}, function (error) {
alert('OKSDK error while requesting access_token ' + OKSDK.Util.toString(error));
});
}
});
window.addEventListener('message', function (event) {
var data = JSON.parse(event.data || "");
if (data.type == 'oauth') {
// we are in original page, emulate hash response from OAUTH and proceed to initialization
config.location_hash = '#access_token=' + data['access_token'] + '&session_secret_key=' + data['session_secret_key'];
OKSDK.init(config, function () {
document.getElementById('oauth_login').style.display = 'none';
OKSDK.REST.call('users.getCurrentUser', null, function (status, data, error) {
if (status == 'ok') {
document.getElementById('content').innerHTML = 'Hello World and hi, ' + data.name + '.';
} else {
alert('Unable to retrieve current user ' + OKSDK.Util.toString(error));
}
});
}, function (error) {
alert('OKSDK error while accepting access_token ' + OKSDK.Util.toString(error));
});
}
}, false);
function startOAuthLogin() {
// open a popup window with same html as we are, with an argument to proceed with OAUTH
window.open(location.origin + location.pathname + '?run_oauth=true');
}
</script>
</body>
</html>