K
Kinde7d ago
samwell

`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:
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" },
});
});
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:
{
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)
}
{
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!
3 Replies
Ages
Ages7d ago
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}); }
- Ensure KINDE_SUBDOMAIN includes the full URL (e.g., https://your-subdomain.kinde.com). - Verify connectivity to the Kinde endpoint using tools like curl or Postman. - Check server logs and intermediary proxies for additional error details. - Refer to Kinde's documentation for further guidance: https://docs.kinde.com/developer-tools/about/using-kinde-without-an-sdk Implement these changes, and let us know if you encounter further issues
Kinde docs
Using Kinde without an SDK
Our developer tools provide everything you need to get started with Kinde.
samwell
samwellOP6d ago
That seems to have worked! Thank you very much!
Ages
Ages5d ago
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.

Did you find this page helpful?