Skip to content

Instantly share code, notes, and snippets.

@ball-hayden
Created March 16, 2017 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ball-hayden/8e115941eff4866aacc5ec4289f04e54 to your computer and use it in GitHub Desktop.
Save ball-hayden/8e115941eff4866aacc5ec4289f04e54 to your computer and use it in GitHub Desktop.
FreeAgent JS API Test
<html>
<body>
<script>
var clientId = "";
var clientSecret = "";
var redirectUri = "http://localhost:8081/"
var params = window.location.search.split("?")
var token = null;
if(params[1]) {
params = params[1].split("&");
token = params[0].split("=")[1];
}
if(!token) {
window.location = `https://api.freeagent.com/v2/approve_app?response_type=code&client_id=${clientId}&redirect_uri=${redirectUri}`;
} else {
fetch(`https://api.freeagent.com/v2/token_endpoint?code=${token}&grant_type=authorization_code&redirect_uri=${redirectUri}`, {
method: "POST",
headers: {
'Authorization': 'Basic ' + btoa(`${clientId}:${clientSecret}`),
'Content-Type': 'application/x-www-form-urlencoded',
}
}).then(function(response) {
return response.json();
}).then(function(token_data) {
return fetch("https://api.freeagent.com/v2/company.json", {
headers: {
Authorization: `Bearer ${token_data.access_token}`
},
credentials: "omit",
})
}).then(function(response) {
return response.json();
}).then(function(body) {
document.write(JSON.stringify(body));
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment