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

  1. Set grantType to one of the supported values ('client_credentials' or 'password') on the config.
  2. Default the field explicitly in your config builder so an unset value is impossible.
  3. 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

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


AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13). Data as JSON: /api/errors/2aa2a014b0b88df6. Report an issue: GitHub.