To make an API call to Fedena the following steps need to be followed.
1. Generate Access Token
- Register the Client App in Fedena
- Click on settings -> Manage Clients.
- Click on New to add a new client.
- Enter name, redirect uri and check on verified.
- This will generate the Client Id & Client Secret which will be used to generate the access token
- Generate the token
- Pass the client id, secret, username and password to generate the access token.
- A sample code is given below:
function get_token(username,password){
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://your_fedena_domain.com/oauth/token", true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange=function(e)
{
if (xhr.readyState==4 && xhr.status==200)
{
console.log(e.target.responseText);
}
}
token=xhr.send("client_id=53a305cab608748as64eb6224831dab772105a989d8e454d4a8d8183afb560c414e2ef&client_secret=95318fad811212710db49aec17353c5983c10a1454cheadfthisis25sa25mp25le26a95e19c7c87dec332&grant_type=password&username="+username+"&password="+password+"&redirect_uri=http://localhost:3001/authorize");
return(token);
}
- Call the above function with the username and password as parameters.
- Then call the API with the token and path (to get or post data) as parameters. A sample function is given below:
function callApi(token, path)
{
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function(evt)
{
console.log("xhr.readyState :: "+httpRequest.readyState);
console.log("xhr.status :: "+httpRequest.status);
if (httpRequest.readyState==4 && httpRequest.status==200)
{
console.log(evt.target.responseText);
}
}
httpRequest.open('GET', 'http://your_fedena_domain.com/api/'+path);
httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
httpRequest.setRequestHeader('Authorization', 'Token token="'+token+'"');
httpRequest.send();
}