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!