apolloconfig/apollo · error · BadRequestException

Token name can not be blank

Error message

Token name can not be blank

What it means

Thrown by UserTokenService.validateCreateRequest() when the UserTokenCreateRequest is null or its name field is blank (null, empty, or whitespace-only, checked via StringUtils.isBlank). This is the first validation in createToken(), called before expires, scope, and operator checks. BadRequestException → HTTP 400.

Source

Thrown at apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/UserTokenService.java:359

    if (UserTokenOperation.APP_CREATE.equals(operation)) {
      return userPermissionValidator.hasCreateApplicationPermission();
    }
    if (UserTokenOperation.USER_MANAGE.equals(operation)) {
      return userPermissionValidator.hasManageUsersPermission();
    }
    if (UserTokenOperation.SYSTEM_ADMIN.equals(operation)) {
      return userPermissionValidator.isSuperAdmin();
    }
    return false;
  }

  private Set<String> emptyToNull(Set<String> values) {
    return values == null || values.isEmpty() ? null : values;
  }

  private void validateCreateRequest(UserTokenCreateRequest request) {
    if (request == null || StringUtils.isBlank(request.getName())) {
      throw new BadRequestException("Token name can not be blank");
    }
    if (request.getRateLimit() != null && request.getRateLimit() < 0) {
      throw BadRequestException.rateLimitIsInvalid();
    }
    if (request.getNamespaces() == null) {
      return;
    }
    for (UserTokenNamespaceScope namespaceScope : request.getNamespaces()) {
      if (namespaceScope == null) {
        throw new BadRequestException("Token namespace scope can not be null");
      }
    }
  }

  private void validateUserEnabled(String userId) {
    UserInfo userInfo = userService.findByUserId(userId);
    if (userInfo == null) {
      throw BadRequestException.userNotExists(userId);

View on GitHub (pinned to d95fc18d11)

Solutions

  1. Set a non-blank name on the UserTokenCreateRequest before calling createToken().
  2. Add client-side validation to require a token name before form submission.
  3. If building the request programmatically, ensure request.setName() is called with a meaningful value.

Example fix

// before
UserTokenCreateRequest request = new UserTokenCreateRequest();
request.setOperations(Set.of(UserTokenOperation.CONFIG_READ));
// after
UserTokenCreateRequest request = new UserTokenCreateRequest();
request.setName("ci-deploy-token");
request.setOperations(Set.of(UserTokenOperation.CONFIG_READ));
Defensive patterns

Strategy: validation

Validate before calling

if (request == null || StringUtils.isBlank(request.getName())) {
    throw new IllegalArgumentException("Token name is required");
}
userTokenService.createToken(request, operator);

Type guard

static boolean hasValidTokenName(UserTokenCreateRequest request) {
    return request != null && !StringUtils.isBlank(request.getName());
}

Try / catch

try {
    userTokenService.createToken(request, operator);
} catch (BadRequestException e) {
    if (e.getMessage().contains("Token name can not be blank")) {
        return Response.status(400).entity("A non-blank token name is required").build();
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling createToken(request, operator) with request=null, request.getName()=null, request.getName()="", or request.getName()=" ". This is typically a client-side omission when POSTing to the token creation endpoint.

Common situations: Frontend form submits without filling in the token name field. API client constructed programmatically without setting the name. JSON deserialization produces a request object with a missing name field due to a schema mismatch.

Related errors


AI-assisted analysis of apolloconfig/apollo@d95fc18d11 (2026-08-14). Data as JSON: /api/errors/75bdea4fb4418508. Report an issue: GitHub.