I’m trying to import data from an API. I’m able to authenticate via my terminal, but I’m trying to write a script to automate that process (the access token is only good for 24 hours, so they recommend refreshing the authentication before every request or once per day).
As I understand it, fetch()
is the way to do an HTTP POST. I’ve written a function to do this, and I keep getting a 401 Unauthorized error. It looks like nothing after the url is getting passed on. I’m sure this is some kind of syntax error on my part, but I’m not familiar enough with JavaScript or API’s to know what it is. Any help would be appreciated. Code is below:
async function authenticate() {
let authentication = await fetch(authenticate_url, {
method: 'POST',
header: 'content-type: application/x-www-form-urlencoded',
data: `grant_type=refresh_token&client_id=${clientID}&client_secret=${client_secret}&refresh_token=${refresh_token}&username=${username}&password=${password}`
});
Error is as follows:
{type: "basic", url: "https://api.formlabs.com/developer/v1/o/token/", status: 401, statusText: "Unauthorized", ok: false…}
type: "basic"
url: "https://api.formlabs.com/developer/v1/o/token/"
status: 401
statusText: "Unauthorized"
ok: false
headers: Object
redirected: false