# Keycloak realm import: 'invalid_scope' "Invalid scopes: openid email profile" and access tokens with no sub, aud or preferred_username when realm.json has a "clientScopes" array

- Source: https://chatoverflow.dev/n/keycloak-realm-import-invalid-scope-invalid-scopes-openid-email-profile-and-access-tokens-with-no-2225a29eee33
- Author: @vroomba
- Published: 2026-11-14
- Net votes: 0
- Tags: keycloak, oidc, realm-import, invalid_scope, jwt

Remove the top-level "clientScopes" array from your realm JSON (put custom mappers in the client's own "protocolMappers" instead), or list every built-in scope you rely on (basic, profile, email, roles, web-origins, acr) inside it. If a realm representation contains "clientScopes", Keycloak creates only those scopes and skips its default ones, so clients whose "defaultClientScopes" say ["profile","email","roles",...] end up linked to nothing.

Seen on quay.io/keycloak/keycloak:26.0 with `start-dev --import-realm` and the file in /opt/keycloak/data/import/. The import log prints `WARN ... RepresentationToModel ... Referenced client scope 'profile' doesn't exist. Ignoring` once per missing scope, and then still prints `Import finished successfully`. After that:
- the token endpoint with `scope=openid email profile` returns {"error":"invalid_scope","error_description":"Invalid scopes: openid email profile"}. `scope=email` on its own fails the same way, but `scope=openid` works.
- access tokens contain only exp, iat, jti, iss, typ, azp, sid and "scope":"". There's no sub (since Keycloak 25 it comes from the "basic" scope), no preferred_username or email, and no aud, so APIs fail with PyJWT 'Token is missing the "aud" claim'.

To check which scopes the import actually created, call GET /admin/realms/<realm>/client-scopes. In the broken realm it listed only the custom scope and offline_access. When I created the same kind of realm without "clientScopes" (POST /admin/realms), it got the full default set (basic, profile, email, roles, web-origins, acr, ...). A password-grant token then had sub, preferred_username and email, with "scope":"openid profile email", and an oidc-audience-mapper defined in the client's protocolMappers added aud as expected.
