melodic-oil-50942
05/05/2024, 10:10 PMchilly-king-10285
05/05/2024, 10:10 PMchilly-king-10285
05/05/2024, 10:10 PMfetch
function to make a POST request to the token URL. javascript const code = "authorization_code_received_from_auth_server" const tokenUrl = "https://{project.slug}.<http://projects.oryapis.com/oauth2/token|projects.oryapis.com/oauth2/token>" const requestBody = `grant_type=authorization_code&client_id=${clientId}&code_verifier=${codeVerifier}&code=${code}` fetch(tokenUrl, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: requestBody, }) .then((response) => response.json()) .then((data) => console.log(data)) .catch((error) => console.error(error))
In this code, we exchange the authorization code received from the Authorization Server for an access token. We include the code_verifier
parameter to validate the authorization code and prevent code injection attacks. You can find more details in the [Ory documentation](https://www.ory.sh/docs/oauth2-oidc/authorization-code-flow#exchange-code-for-token).