I am trying to retrieve a token from Anypoint Mulesoft API, but I am not sure which link to use,I have used both links and they are not working.
The info provided was:
Client ID
Client Secret
Grant Types
Redirect URLS :
https://api-notebook.anypoint.mulesoft.com/authenticate/oauth.html, https://anypoint.mulesoft.com/exchange/oauthCallback.html
Frontend
useEffect(() => {
getToken();
}, []); // <- add empty brackets here. Runs only on the first render
const bodyConfig = {
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
grant_type: process.env.GRANT_TYPE,
};
async function getToken() {
try {
const response = await fetch(
"https://api-notebook.anypoint.mulesoft.com/authenticate/oauth.html",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(bodyConfig),
}
);
if (!response) {
throw new Error("Authentication failed");
}
const result = await response.json();
console.log(result.token);
} catch (error) {
console.log(error);
}
I tried both URLS for the POST
https://api-notebook.anypoint.mulesoft.com/authenticate/oauth.html, https://anypoint.mulesoft.com/exchange/oauthCallback.html
I tried to use
fetch and axios
async function getToken() {
axios
.post(
"https://api-notebook.anypoint.mulesoft.com/authenticate/oauth.html",
{
client_id: process.env.NZ_POST_CLIENT_ID,
client_secret: process.env.NZ_POST_CLIENT_SECRET,
grant_type: process.env.NZ_POST_GRANT_TYPE,
}
)
.then((response) => {
console.log(response.token);
})
.catch((error) => {
console.log("something went wrong :(");
console.log(error);
});
}