我正在尝试使用Node js通过Lambda函数中的 axios 通过POST调用创建 Cognito JWtoken ,但出现错误 400错误的请求。 同时,在Postman中工作正常。
代码:
await axios.post('https://smartfactoryfabric-dev.auth.us-east-1.amazoncognito.com/oauth2/token', {
grant_type: grantType, // This is the body part
redirect_uri: redirectURI,
client_id: clientid,
code: 'fb06a2dd-XXXXXXXXX-f186c6302806'
}, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + authValue
}
})
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error.response)
});
答案 0 :(得分:0)
您需要URL将请求的主体编码为单个字符串。您可以使用querystring.stringify
来完成此操作。
let data = querystring.stringify({
grant_type: 'grantType',
redirect_uri: 'redirectURI',
client_id: 'clientid',
code: 'fb06a2dd-XXXXXXXXX-f186c6302806'
})
=> 'client_id=clientid&code=fb06a2dd-XXXXXXXXX-f186c6302806&grant_type=grantType&redirect_uri=redirectURI'
然后您的请求将如下所示:
await axios.post('https://smartfactoryfabric-dev.auth.us-east-1.amazoncognito.com/oauth2/token',
data,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + authValue
}
})