fetch('https://api.petfinder.com/v2/oauth2/token', {
method: 'POST',
body: 'grant_type=client_credentials&client_id=' + Secret('Petfinder API Key') + '&client_secret=' + Secret('Petfinder API Secret'),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(resp => {
return resp.json()
}).then(data => {
console.log('token', data)
cache.token = data.access_token;
cache.tokenType = data.token_type;
cache.expires = new Date().getTime() + (data.expires_in * 1000)
return data
}).then(data => {
return fetch ('https://api.petfinder.com/v2/animals?organization=' + org + '&status=' + status, {
headers: {
'Authorization': `${data.token_type} ${data.access_token}`,
'Content-Type': 'application/x-www/form-urlencoded'
}
})
}).then(resp => {
return resp.json()
}).then(data => {
console.log('pets', data)
return data
}).catch(err => {
console.log('something went wrong', err)
})