我正在将最新版本的react与axios结合使用,并希望从aws / cognito获取身份验证令牌。因此,我有我的客户和客户秘密。当我发送curl请求时,它可以正常工作,但是当我通过axios发送请求时,我总是收到状态405的响应。
我的代码如下:
...
axios({
url: 'https://xyz.amazoncognito.com/oauth2/token?grant_type=client_credentials',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'client_id': '***************',
'client_secret': '****************'
'redirect_uri': 'http://localhost:4200'
}
})
.then((response) => {
console.log(response);
}, (error) => {
console.log(error);
});
我没有将client_id,client_secret和redirect_uri设置为标题,而是将它们添加到了
的url中...grant_type=client_credentials&client_id=************&client_secret=*************&redirect_uri=http%3A%2F%2Flocalhost%3A4200
,结果相同。任何想法,我在做什么错?附带一提:我对所有的api请求都使用axios,因此在这种情况下,我也想留在axios。
感谢和亲切问候,
Balu
答案 0 :(得分:0)
您没有正确传递所需的参数。看看这里的例子: https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html
所需的标题为:
授权 如果向客户端发布了机密,则客户端必须通过基本HTTP授权在授权标头中传递其client_id和client_secret。秘密是基本Base64Encode(client_id:client_secret)。
内容类型 必须始终为“ application / x-www-form-urlencoded”。
其他信息将作为请求参数传递。
这就是说,您不应在客户端(React应用程序)上存储客户端和客户端机密。如果这是在客户端上公开的,则任何人都可以获取您的客户端ID和客户端机密,并获取Cognito令牌。