Getting clientId
and authToken
- Login to your system with your admin-user account
- Go to the API client list:
- Click
create new
button
- Type API client name and check
Active
checkbox
- Save
- Copy values:
Name
as clientId
Secret
as authToken
Getting a token:
function getToken() {
return new Promise(function (resolve, reject) {
fetch("/security/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
// Client name
clientId: "sample_client",
// Client token
authToken:
"29c327e0ed2a5d22f8faa9edbab7ee400951502828172eaa5571d311da5911d6",
// User login
login: "test",
// User password
password: "test1",
}),
})
.then(function (response) {
// Login failed
if (response.status !== 200) {
reject(response);
}
// Examine the text in the response - return token
response.json().then(function (data) {
resolve(data.token);
});
})
.catch(function (err) {
reject("Fetch Error :-S", err);
});
});
}
Use a token to get products:
function getProducts(token) {
return new Promise(function (resolve, reject) {
fetch("/api/products", {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
})
.then(function (response) {
resolve(response);
// ...
})
.catch(function (err) {
reject("Fetch Error :-S", err);
});
});
}
getToken().then(function (token) {
getProducts(token);
});