我通过使我的代码如下所述解决了 AWS Cognito oauth2/token 端点中的此错误 405 方法不允许错误,并且它运行良好。
我从这个链接中获得了帮助,并使用正确的格式在 fetch 请求中提到了 header 和 body 参数:
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)
})
我使用配置文件来保存变量。
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"
}