1 Answer
- Newest
- Most votes
- Most comments
0
Figured out the POST body isn't JSON but a query string format:
// redirect the user to this URI
function getAuthUri() {
const state = crypto.randomBytes(20).toString('hex')
const uri = `${AUTH_DOMAIN}/login?client_id=${AUTH_CLIENT_ID}&state=${state}&response_type=code&redirect_uri=${AUTH_CALLBACK_URI}`
return uri
}
// after extracting the code from the authentication redirect (to AUTH_CALLBACK_URI), use this to get the access token
async function getAccessToken(code) {
const config = {
headers: {
'Authorization': 'Basic ' _ Buffer.from(AUTH_CLIENT_ID _ ':' + AUTH_CLIENT_SECRET).toString('base64'),
'Content-Type': 'application/x-www-form-urlencoded'
}
}
const body = `grant_type=authorization_code&client_id=${AUTH_CLIENT_ID}&code=${code}&redirect_uri=${AUTH_CALLBACK_URI}`
try {
const response = await axios.post(`${AUTH_DOMAIN}/oauth2/token`, body, config)
return response.data.access_token
}
catch (err) {
console.log(err.response.data.error)
throw err
}
}
// use the access token
async function getUser(accessToken) {
const config = {
headers: {
Authorization: `Bearer ${accessToken}`
}
}
const response = await axios.get(`${AUTH_DOMAIN}/oauth2/userInfo`, config)
const user = response.data
return user
}
answered 2 years ago
Relevant content
- asked 4 months ago
- Accepted Answerasked 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 2 years ago
i have encountered same invalid_request error and applied your answer but still getting the same error.