I resolved this error 405 method not allowed error in AWS Cognito oauth2/token
endpoint by making my code as below mentioned, and it worked fine.
I took help from this link and use the correct format to mention both header and body parameters in the fetch request:
https://formcarry.com/documentation/fetch-api-example
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": `Basic ${authData}`,
"Accept": "application/json"
},
body: `grant_type=${config.grant_type}&code=${code}&client_id=${config.clientId}&redirect_uri=${config.loginRedirectUri}`
}
fetch(`${config.domainUrl}/oauth2/token`, requestOptions)
.then(response => response.json())
.then(data => {
sessionStorage.setItem("access_token",data.access_token)
fetchUserDetails(data.access_token)
})
I used a config file to save variables.
const config = {
domainUrl: "https://domainname.auth.origin.amazoncognito.com",
clientId: "xxxxxxxxxxxx",
loginRedirectUri: "http://localhost:8000/redirecturi",
grant_type: "authorization_code",
logoutUri: "http://localhost:8000",
clientSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}