usebruno/bruno · error · Error
Grant type is required for OAuth2
Error message
Grant type is required for OAuth2
What it means
Thrown by the main getOAuth2AccessToken entry point when oauth2Config.grantType is falsy. The grant type selects which token flow to run, so the dispatcher rejects the call before consulting the token store. This is the outermost guard, hit before any flow-specific validation.
Source
Thrown at packages/bruno-requests/src/auth/oauth2-helper.ts:333
/**
* Manages OAuth2 token retrieval and storage
*/
export const getOAuth2Token = async (oauth2Config: OAuth2Config, tokenStore: TokenStore, verbose: string, axiosInstance?: AxiosInstance): Promise<string | null> => {
const {
grantType,
accessTokenUrl,
credentialsId = 'default',
autoFetchToken = true,
tokenSource = 'access_token'
} = oauth2Config;
if (verbose) {
debug.enable('oauth2');
}
if (!grantType) {
throw new Error('Grant type is required for OAuth2');
}
if (!accessTokenUrl) {
throw new Error('Access token URL is required for OAuth2');
}
if (!['client_credentials', 'password'].includes(grantType)) {
throw new Error(`Unsupported grant type: ${grantType}. Supported types: client_credentials, password`);
}
// Check if we already have credentials stored
const existingToken = await tokenStore.getCredential({ url: accessTokenUrl, credentialsId });
if (existingToken) {
// Check if token is expired
if (!isTokenExpired(existingToken)) {
// Token is valid, use it
return tokenSource === 'id_token' ? existingToken.id_token : existingToken.access_token;View on GitHub (pinned to 9bdd81c7bd)
Solutions
- Set grantType to one of the supported values ('client_credentials' or 'password') on the config.
- Default the field explicitly in your config builder so an unset value is impossible.
- Validate the config shape before calling the helper (see validationCode).
Example fix
// before
const config = { accessTokenUrl: url, clientId: id, clientSecret: secret };
await getOAuth2AccessToken(config, tokenStore);
// after
const config = {
grantType: 'client_credentials',
accessTokenUrl: url,
clientId: id,
clientSecret: secret
};
await getOAuth2AccessToken(config, tokenStore); Defensive patterns
Strategy: type-guard
Validate before calling
if (!config.grantType) throw new Error('grantType must be set on OAuth2Config');
await getOAuth2AccessToken(config, tokenStore); Type guard
function hasGrantType(c) {
return typeof c.grantType === 'string' && c.grantType.length > 0;
} Try / catch
try { await getOAuth2AccessToken(config, tokenStore); }
catch (e) { if (e.message === 'Grant type is required for OAuth2') { /* default grant */ } else throw e; } Prevention
- Default grantType in your config builder so it can never be unset.
- Use a discriminated union keyed on grantType to get compile-time safety.
- Reject config objects with unknown grant types at the boundary.
When it happens
Trigger: Calling getOAuth2AccessToken with a config object whose grantType field is missing, null, undefined, or empty string.
Common situations: Config deserialized from JSON that omitted grantType; UI radio button for grant type never selected; programmatic caller built a partial config and forgot the grant type selector.
Related errors
- Access Token URL is required for OAuth2 client credentials f
- Client ID is required for OAuth2 client credentials flow
- Access Token URL is required for OAuth2 password credentials
- Username is required for OAuth2 password credentials flow
- Password is required for OAuth2 password credentials flow
AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13).
Data as JSON: /api/errors/2aa2a014b0b88df6.
Report an issue: GitHub.