-
-
Save harryhan24/10e4075fcc6e1eeb4d1e6b851374bdbc to your computer and use it in GitHub Desktop.
How to get an access_token from AWS Cognito using Axios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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