React和Axios获得AWS客户端证书

问题描述 投票:0回答:1

我正在使用最新版本的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);
       });

而不是将header_id,client_secret和redirect_uri设置为标题,而是将它们添加到url中,如

...grant_type=client_credentials&client_id=************&client_secret=*************&redirect_uri=http%3A%2F%2Flocalhost%3A4200

具有相同的结果。任何想法,我在做什么错?附带说明:我正在使用axios来处理我的所有api请求,因此在这种情况下我也想留在axios上。

感谢和亲切的问候,

Balu

reactjs axios amazon-cognito credentials
1个回答
0
投票

您没有正确传递所需的参数。在这里看看例子:https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html

所需的标题将是:

授权如果向客户端颁发了机密,则客户端必须通过基本HTTP授权在授权标头中传递其client_id和client_secret。机密是Basic Base64Encode(client_id:client_secret)。

内容类型必须始终为'application / x-www-form-urlencoded'。

其他信息将作为请求参数传递。

话虽如此,您不应将客户端和客户端机密存储在客户端(React应用程序)。如果这在客户端上公开,则任何人都可以获取您的客户端ID和客户端密码并获取Cognito令牌。

© www.soinside.com 2019 - 2024. All rights reserved.