Welcome to the Kinde community

Updated 3 days ago

`oauth2/token` endpoint returns 502 Bad Gateway error

I am trying to call the /oauth2/token endpoint from a Hono.js server (running in a Bun environment) and am getting a 502 Bad Gateway response. This is my code:

Plain Text
import { Hono, type Context } from "hono";
import { GrantType } from "@kinde-oss/kinde-typescript-sdk";

const api = new Hono();

api.get("/my-route-path", async (c: Context) => {
  const url = new URL(c.req.url);
  const authCode = url.searchParams.get("code");
  const response = await fetch(`${KINDE_SUBDOMAIN}/oauth2/token`, {
    method: "POST",
    body: JSON.stringify({ 
      client_id: KINDE_CLIENT_ID,
      client_secret: KINDE_CLIENT_SECRET,
      grant_type: GrantType.AUTHORIZATION_CODE,
      redirect_uri: KINDE_REDIRECT_URL,
      code: authCode,
    }),
    headers: { "Content-Type": "application/json" },
  });
});


This is the error response that I am getting:


Plain Text
{
  ok: false,
  url: "https://my-kinde-subdomain/oauth2/token",
  status: 502,
  statusText: "Bad Gateway",
  headers: Headers {
    "date": "Tue, 14 Jan 2025 02:03:49 GMT",
    "content-type": "text/html",
    "content-length": "122",
    "connection": "keep-alive",
    "server": "awselb/2.0",
  },
  redirected: false,
  bodyUsed: false,
  Blob (122 bytes)
}


I am following the docs on this page: https://docs.kinde.com/developer-tools/about/using-kinde-without-an-sdk/

Has anyone run into this? Thank you!
A
s
3 comments
Hi @samwell ,

You're encountering a 502 Bad Gateway error when making a POST request to the /oauth2/token endpoint with the following payload:

{
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET",
"grant_type": "authorization_code",
"redirect_uri": "YOUR_REDIRECT_URI",
"code": "AUTHORIZATION_CODE"
}

This suggests the server acting as a gateway received an invalid response from the upstream server.

This can happen if the request parameters are sent as query parameters instead of in the POST body. Ensure all parameters are included in the body and the Content-Type header is set to application/x-www-form-urlencoded.

Example Fix:

const response = await fetch(${KINDE_SUBDOMAIN}/oauth2/token, {
method: "POST",
body: new URLSearchParams({
client_id: KINDE_CLIENT_ID,
client_secret: KINDE_CLIENT_SECRET,
grant_type: "authorization_code",
redirect_uri: KINDE_REDIRECT_URL,
code: authCode,
}),
headers: { "Content-Type": "application/x-www-form-urlencoded" },
});
if (response.ok) {
const data = await response.json();
// Process data
} else {
console.error(Error: ${response.status} ${response.statusText});
}

Implement these changes, and let us know if you encounter further issues
That seems to have worked! Thank you very much!
Hi @samwell ,

I'm glad to hear that the solution worked and resolved your issue. We'll go ahead and close this ticket. If you have any more questions in the future, feel free to open a new one.
Add a reply
Sign up and join the conversation on Discord