Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
How to get access token from Cognito Endpoint

I am trying to implement an API request to Cognito API endpoint in plain Javascript. I am not using any frameworks.....

My Challenge is to get user information from Cognito's endpoint GET /oauth2/userInfo

https://docs.aws.amazon.com/cognito/latest/developerguide/userinfo-endpoint.html

From this, I would need the <access_token>.

What I have is a little web application that talks with a SaaS-Platform to perform authentication to a messenger via Cognito Authorization code grant.

While embedding the messenger I have subscribed my code to an event, which delivers me the authCode which looks like this: 8595abc1-44e5-435d-9e78-fc7d274cc046

What I am trying is to talk to another endpoint of Cognito to provide the AuthCode and receive the access token POST /oauth2/token https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html

neither in postman nor in my code i am able to manage this task. The error O am receiving from Cognito is invalid_grant which means basically

Authorization code has been consumed already or does not exist.

This is my code. Not very clear but I am still modify the code here and there but overall with no success

const http = new XMLHttpRequest();
http.open("POST", "https://<poolName>.auth.<region>.amazoncognito.com/oauth2/token");
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.setRequestHeader("Authorization", "Basic <Base64Encode(client_id:client_secret>");
http.send("grant_type=authorization_code&client_id=<clientID>&code=108cad7f-0505-48ee-9836-363033bf2004&redirect_uri=<myURI>");
http.onload = () => console.log("result" + http.responseText);

Can you please support me or what might be wrong in my approach to exchange with Cognito the auth-code for being able to talk to the userInfo-Endpoint?

Many thanks!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!