Skip to content

Instantly share code, notes, and snippets.

@ejdoh1
Created November 26, 2019 06:46
Show Gist options
  • Save ejdoh1/569bc7d43c79c7c1a80872588431994c to your computer and use it in GitHub Desktop.
Save ejdoh1/569bc7d43c79c7c1a80872588431994c to your computer and use it in GitHub Desktop.
How to get an access_token from AWS Cognito using Axios
// Get AWS Congito access token using node-fetch and axios
const axios = require('axios');
const fetch = require('node-fetch');
const url = 'https://REPLACE_ME.auth.ap-southeast-2.amazoncognito.com/oauth2/token';
const clientId = 'REPLACE_ME';
const clientSecret = 'REPLACE_ME';
const body = `client_id=${clientId}&client_secret=${clientSecret}&grant_type=client_credentials`
// with node-fetch
fetch(
url,
{
method: 'post',
body: body,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + Buffer.from(clientId + ":" + clientSecret).toString('base64')
},
}
).then(
res => res.json()
).then(
json => console.log(json)
);
// with axios
axios.post(
url,
body,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + Buffer.from(clientId + ":" + clientSecret).toString('base64')
},
})
.then(function (response) {
console.log(response.data.access_token);
})
.catch(function (error) {
console.log(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment