Using Thunder Client in VSCODE I can send a GET request using an API token I created on the advanced setting page in SS. However, when I use the js FETCH function and apply the exact same headers with the same token I get a 401 error.
My example code is below, any ideas on why the 401 error?
var url = "https://api.squarespace.com/1.0/profiles?sortField=email&sortDirection=asc"
var apiKey = "Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
var userAgent = "myAgent"
var myInit = {
method: 'GET',
headers: {
'Accept': '*/*',
'Authorization': apiKey ,
'User-Agent': userAgent ,
'Content-Type': 'application/json',
},
mode: 'no-cors',
cache: 'no-cache',
}
var myRequest = new Request(url)
fetch(myRequest, myInit)
.then(response => {
console.log(`# of profiles: ${Object.keys(response.json).length}`)
if(!response.ok){
console.log(`in on error from GET`)
console.log(`response status: ${response.status}`)
console.log(`response body: ${response.body}`)
throw new Error(response)
}
return response.json()
})
.catch(error => {
//do some custom error handling
})
.then(theJson => {
console.log(`in on getting json`)
//build a data table out of the json data