-
-
Save ball-hayden/8e115941eff4866aacc5ec4289f04e54 to your computer and use it in GitHub Desktop.
FreeAgent JS API Test
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
<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