index.js
class AuthClient {
constructor(username, clientId, clientSecretId, grantType = "client_credentials") {
this.url = 'https://auth.developers.ar.ua.la/v2/api/auth/token';
this.payload = {
username: username,
client_id: clientId,
client_secret_id: clientSecretId,
grant_type: grantType
};
}
async getToken() {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(this.payload)
};
try {
const response = await fetch(this.url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error:', error);
throw error;
}
}
}
// Uso del constructor
const username = 'username';
const clientId = 'clientId';
const clientSecretId = 'clientSecretId';
const authClient = new AuthClient(username, clientId, clientSecretId);
authClient.getToken()
.then(token => {
console.log('Token:', token);
})
.catch(error => {
console.error('Error:', error);
});