discordjs/discord.js · error · Error
response.statusText
Error message
response.statusText
What it means
authorize() performs the OAuth2 token exchange with Discord and throws Error(response.statusText) whenever the HTTP response is not ok. The message is just the status text (e.g. 'Unauthorized', 'Bad Request'), meaning the credentials or request body were rejected.
Source
Thrown at packages/rpc/src/client.ts:369
code,
grant_type: 'authorization_code',
};
if (this.options.redirectUri) {
jsonBody.redirect_uri = this.options.redirectUri;
}
const response = await fetch(`${RouteBases.api}${Routes.oauth2TokenExchange()}`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams(jsonBody),
...options,
});
if (!response.ok) {
throw new Error(response.statusText);
}
const data = (await response.json()) as RESTPostOAuth2AccessTokenResult;
if (!('access_token' in data)) {
throw new Error(JSON.stringify(response));
}
return data.access_token;
}
/**
* Authenticate
*
* @param accessToken - access token
*/
public async authenticate(accessToken: string, options?: RequestOptions): Promise<this> {
const { application, user } = await this.request(RPCCommands.Authenticate, { access_token: accessToken }, options);View on GitHub (pinned to a81ed8a306)
Solutions
- Log the full response inside a catch and verify clientId/clientSecret against the Discord developer portal
- Ensure the authorization code is fresh and matches the same client_id, scopes, and redirect_uri used in the authorize step
- Retry later or check status.discordapp.com if the status is 5xx
Example fix
// before
const token = await client.authorize(code, redirect);
// after
try {
const token = await client.authorize(code, redirect);
} catch (err) {
console.error('Token exchange failed:', err.message); // e.g. 'Unauthorized'
} Defensive patterns
Strategy: try-catch
Validate before calling
// ensure credentials are non-empty before attempting the exchange
if (!clientId || !clientSecret) throw new Error('clientId/clientSecret required before authorize()'); Try / catch
try {
await client.login({ clientId, clientSecret, scopes });
} catch (err) {
console.error(`OAuth token exchange failed: ${err.message}`);
// inspect status code, refresh code, and retry once
} Prevention
- Use each authorization code exactly once and immediately
- Keep clientId/clientSecret/redirect_uri identical across authorize and token steps
- Monitor for 5xx and implement one retry with backoff
When it happens
Trigger: login() with scopes where the token endpoint returns 4xx/5xx: wrong clientId/clientSecret, invalid authorization code, mismatched redirect_uri, or Discord being down.
Common situations: Expired or single-use authorization code reused, swapped client id/secret, wrong scopes requested, or network proxies returning error statuses.
Understand the failure class
Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.
Related errors
- HTTPError(status, res.statusText, method, url, requestData)
- JSON.stringify(response)
- A client secret must be provided for authorization if scopes
- No compatible encryption modes. Available include: ${options
- Malformed IP address
AI-assisted analysis of discordjs/discord.js@a81ed8a306 (2026-08-30).
Data as JSON: /api/errors/022990d4c89719e5.
Report an issue: GitHub.