Archived
85 lines
2.5 KiB
HTML
85 lines
2.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<title>OAuth 2 User Agent Authentication Flow Demo</title>
|
|
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
|
|
<script type="text/javascript" charset="utf-8">
|
|
$(function () {
|
|
var extractToken = function(hash) {
|
|
var match = hash.match(/access_token=(\w+)/);
|
|
return !!match && match[1];
|
|
};
|
|
|
|
var setting =
|
|
{
|
|
'host': "london2011.ez.no"
|
|
, 'clientId': "77d9f303efe45b6bfdf6a3e8e5860952"
|
|
};
|
|
|
|
var authHost = "http://" + setting.host;
|
|
var resourceHost = "http://" + setting.host + "/api/ezp/v1";
|
|
|
|
var endUserAuthorizationEndpoint = authHost + "/oauth/authorize";
|
|
|
|
var token = extractToken(document.location.search);
|
|
if (token) {
|
|
$('div.authenticated').show();
|
|
|
|
$('span.token').text(token);
|
|
|
|
$.ajax({
|
|
url: resourceHost + '/content/node/2'
|
|
, beforeSend: function (xhr) {
|
|
xhr.setRequestHeader('Authorization', "OAuth " + token);
|
|
xhr.setRequestHeader('Accept', "application/json");
|
|
}
|
|
, success: function (response) {
|
|
var container = $('span.user');
|
|
if (response) {
|
|
container.text(response);
|
|
} else {
|
|
container.text("An error occurred.");
|
|
}
|
|
}
|
|
});
|
|
} else {
|
|
$('div.authenticate').show();
|
|
var returnTo = window.location;
|
|
returnTo = 'http://london2011.ez.no/api/ezp/v1';
|
|
|
|
var authUrl = endUserAuthorizationEndpoint +
|
|
"?response_type=token" +
|
|
"&client_id=" + setting.clientId +
|
|
"&redirect_uri=" + encodeURIComponent( returnTo );
|
|
console.log( 'authUrl: %o', authUrl );
|
|
$("a.connect").attr("href", authUrl);
|
|
}
|
|
});
|
|
</script>
|
|
<style>
|
|
.hidden {
|
|
display: none;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="authenticate hidden">
|
|
<a class="connect" href="">Connect</a>
|
|
</div>
|
|
|
|
<div class="authenticated hidden">
|
|
<p>
|
|
You are using token
|
|
<span class="token">[no token]</span>.
|
|
</p>
|
|
|
|
<p>
|
|
Your data is
|
|
<span class="user">[no username]</span>.
|
|
</p>
|
|
</div>
|
|
</body>
|
|
</html>
|