Skip to content

Instantly share code, notes, and snippets.

@sween
Forked from ejdoh1/get_access_token.js
Created April 28, 2021 15:22
Show Gist options
  • Save sween/55bc8a98c76d3c580b699eb1dfbade25 to your computer and use it in GitHub Desktop.
Save sween/55bc8a98c76d3c580b699eb1dfbade25 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