Last active
November 30, 2022 15:00
-
-
Save sitefinitySDK/23b4887123c34eac20f1c3a22fd6a7db to your computer and use it in GitHub Desktop.
SF_10.1, SF_10.2, SF_11.0, SF_11.1, SF_11.2, SF_12.0, SF_12.1, SF_12.2, SF_13.0, SF_13.1, SF_13.2, SF_13.3, SF_14.0, SF_14.1, SF_14.2, SF_14.3 - https://docs.sitefinity.com/request-access-token-for-calling-web-services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Title</title> | |
<meta charset="utf-8" /> | |
</head> | |
<body> | |
<h1>JS-based resource owner sample</h1> | |
<div> | |
<label>username</label> | |
<input id="username" value="test@test.test" /> | |
</div> | |
<div> | |
<label>password</label> | |
<input id="password" type="password" value="password" /> | |
</div> | |
<div> | |
<button id="getTokenBtn">Get Token with Credentials</button> | |
<button id="getTokenWithRefreshBtn">Get Token with Refresh Token</button> | |
<button id="apiCallBtn">Api Call</button> | |
</div> | |
<div> Token: <label id="token"></label></div> | |
<br/> | |
<div>Refresh Token:<label id="refreshToken"></label></div> | |
<br/> | |
<div>Api Result:<p id="apiResult"></p></div> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> | |
<script> | |
//The token end point from where we can retrieve the access token | |
var tokenEndPoint = "http://yoursitefinitysite/Sitefinity/Authenticate/OpenID/connect/token"; | |
var apiUrl="http://yoursitefinitysite/api/default/newsitems"; | |
var client_id = "testApp"; | |
var client_secret = "secret"; | |
var accessToken; | |
var refreshToken; | |
$("#getTokenBtn").on("click", getToken); | |
$("#getTokenWithRefreshBtn").on("click", getAccessTokenFromRefreshToken); | |
$("#apiCallBtn").on("click", callApi); | |
function getToken() { | |
var username = $('#username').val(); | |
var password = $('#password').val(); | |
//Call that gets the access and refresh token | |
$.ajax({ | |
url:tokenEndPoint, | |
data:{ | |
username:username, | |
password:password, | |
grant_type:'password', | |
scope:'openid offline_access', | |
client_id:client_id, | |
client_secret:client_secret | |
}, | |
method:'POST', | |
success:function(data){ | |
console.log(data.access_token); | |
console.log(data.refresh_token); | |
$('#token').text(data.access_token); | |
$('#refreshToken').text(data.refresh_token); | |
accessToken=data.access_token; | |
refreshToken=data.refresh_token; | |
}, | |
error:function(err){ | |
alert(err.responseText); | |
} | |
}) | |
} | |
//Call that gets new access and refresh token from the current refresh token | |
function getAccessTokenFromRefreshToken() { | |
$.ajax({ | |
url:tokenEndPoint, | |
data:{ | |
refresh_token:refreshToken, | |
grant_type: 'refresh_token', | |
client_id:client_id, | |
client_secret:client_secret | |
}, | |
method:'POST', | |
success:function(data){ | |
console.log(data.access_token); | |
console.log(data.refresh_token); | |
$('#token').text(data.access_token); | |
$('#refreshToken').text(data.refresh_token); | |
accessToken=data.access_token; | |
refreshToken=data.refresh_token; | |
}, | |
error:function(err){ | |
alert(err.responseText); | |
} | |
}) | |
} | |
//Sitefinity Web API call with access token as a bearer token | |
function callApi() { | |
$.ajax({ | |
url:apiUrl, | |
method:'GET', | |
beforeSend:function (xhr) { | |
xhr.setRequestHeader ("Authorization", "Bearer " + accessToken); | |
}, | |
success:function(data){ | |
if(data.value.length!==0){ | |
$("#apiResult").text("Item content:"+ data.value[0].Content) | |
} | |
else{ | |
$("#apiResult").text("No news items"); | |
} | |
}, | |
error:function(err){ | |
alert(err.responseText); | |
} | |
}) | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment