Skip to content

OAuth scope string builder

Pick standard and custom scopes and get the space-separated scope parameter, encoded for a URL or a form body.

Runs in your browser. Nothing you paste is sent to us.

Scopes

Standard (OpenID Connect)
Keycloak client scopes

Separated by spaces, commas or new lines. A sample scope is filled in.

scope parameter

scope
openid profile email orders:read
In a URL (percent-encoded)
scope=openid%20profile%20email%20orders%3Aread
In a form body
scope=openid+profile+email+orders%3Aread
openid
Makes the request an OpenID Connect request: an ID token is issued.
profile
name, family_name, given_name, middle_name, nickname, preferred_username, profile, picture, website, gender, birthdate, zoneinfo, locale, updated_at
email
email, email_verified
orders:read
A scope your server defines.
Default and optional client scopes in Keycloak
Keycloak adds a client’s default client scopes to every token, whether or not they are asked for. Only optional client scopes have to be listed in scope. In a new realm, profile, email, roles, web-origins, acr and basic are default; address, phone, offline_access and microprofile-jwt are optional.

How to use it

  1. Tick the standard OpenID Connect scopes you need.
  2. Add Keycloak client scopes or your own.
  3. Copy the scope string, or the encoded form for a URL or body.

What to know

The scope parameter is a list of scopes separated by spaces. openid turns an OAuth request into an OpenID Connect request; profile, email, address and phone ask for groups of claims.

In Keycloak, a client’s default client scopes are applied whether or not they are requested; only optional client scopes need to be in the scope parameter.

offline_access asks for a refresh token that keeps working after the user’s session ends.

What a scope is

A scope is a name for a slice of access. When an app asks for a token, it lists the scopes it wants. The user may approve them, the server may trim them, and the token that comes back is limited to what was granted.

RFC 6749 section 3.3 defines the scope parameter: a list of scope tokens separated by spaces. The order does not matter. Each token is a case-sensitive string. The specification does not define any scope values itself; the server decides what each one means.

OpenID Connect Core 1.0 adds a few standard scopes on top. The most important is openid, which turns an OAuth request into an OpenID Connect request and makes the server issue an ID token.

The syntax rules

RFC 6749 allows a narrow set of characters in a scope token: any visible ASCII character except the space, the double quote and the backslash. Tokens are separated by single spaces.

  • Valid: openid, orders:read, https://api.example.com/orders.read.
  • Not valid as one scope: orders read (that is two scopes), or a value with " or \.
  • A common mistake: commas between scopes. openid,profile is a single, unknown scope.

In a URL the space is percent-encoded as %20. In a form body it may also appear as +. Both decode to the same space.

Sample scope parameter in three forms (made-up custom scope)
scope:      openid profile email orders:read
in a URL:   scope=openid%20profile%20email%20orders%3Aread
form body:  scope=openid+profile+email+orders%3Aread

The standard OpenID Connect scopes

OpenID Connect Core section 5.4 defines scopes that ask for groups of user claims. Section 11 defines offline_access.

ScopeWhat it asks for
openidRequired for OpenID Connect. An ID token is issued.
profilename, family_name, given_name, middle_name, nickname, preferred_username, profile, picture, website, gender, birthdate, zoneinfo, locale, updated_at.
emailemail and email_verified.
addressThe address claim, a JSON object.
phonephone_number and phone_number_verified.
offline_accessA refresh token that keeps working after the user’s session ends.

Asking for a scope does not guarantee every claim. A user with no phone number on file gets no phone_number, whatever the scope says. The OIDC claims reference describes each claim.

What this tool does and does not do

The scope builder turns a set of choices into a correct scope string.

  • It offers the standard OpenID Connect scopes and the common Keycloak client scopes as checkboxes.
  • It takes your own scopes separated by spaces, commas or new lines.
  • It removes repeats, turns commas into spaces and drops tokens with characters RFC 6749 does not allow.
  • It shows the string plain, encoded for a URL and encoded for a form body.
  • It lists what each chosen scope asks for.

It does not know which scopes your server defines or which ones a client may request. A scope that looks fine here can still be refused with invalid_scope.

How to use the builder

  1. Tick openid if you want an ID token, then the claim scopes you need.
  2. Tick any Keycloak client scopes you want to request explicitly.
  3. Type your own API scopes in the text box.
  4. Copy the scope string, or the URL or form encoding, into your request.
  5. Read the notes for anything that was removed or changed.

Choosing scopes for your app

Sign-in only

An app that only needs to know who the user is can ask for openid and maybe profile and email. Ask for no more than the screens actually show.

Calling your own API

Add API scopes such as orders:read. The API checks the scope claim in the access token before it serves a request. Keep read and write scopes separate, so a client that only reads cannot write.

Background work

A sync job or mobile app that must keep working while the user is away adds offline_access. Leave it out otherwise; long-lived tokens are a bigger loss if stolen.

Machine-to-machine

With client credentials there is no user, so openid and the profile scopes make no sense. Request only the API scopes the service needs, or none and rely on the client’s defaults.

How Keycloak handles scopes

Keycloak models scopes as client scopes. Each client scope bundles protocol mappers, which add claims to tokens, and can carry role scope mappings. A client scope is attached to a client as either default or optional.

  • Default client scopes are applied to every token for that client, whether or not the request asks for them.
  • Optional client scopes are applied only when they appear in the scope parameter.
  • In a new realm, profile, email, roles, web-origins, acr and basic are default; address, phone, offline_access and microprofile-jwt are optional.
  • roles adds realm_access and resource_access; web-origins adds allowed-origins; basic adds sub and auth_time (Keycloak 25 and later).
  • offline_access needs the user to have the offline_access role, which new users get by default.

So in Keycloak, sending scope=openid alone often still returns profile and email claims, because those scopes are defaults. To make them opt-in, move them from default to optional on the client.

Seeing the result in Keycloak

Because default scopes are added silently, it can be hard to predict what a token will hold. Keycloak’s admin console has a preview for this. Open the client, go to Client scopes, then Evaluate. Pick a user and type the scope parameter you plan to send.

The page then lists the client scopes that would apply and shows a generated access token, ID token and UserInfo response for that user. It is the quickest way to check that a mapper adds a claim, or that moving a scope to optional really removes it.

Scope versus audience

A scope says what a token allows. It does not say which API the token is for. Two APIs could both define read, and a token with that scope would work at both.

The audience fixes that. The aud claim names the services that should accept the token, and each service must reject tokens meant for others. RFC 8707 adds a resource parameter so a client can ask for a token for a specific API by URL.

Use both together: scopes for the actions, audience for the target. Naming API scopes with a prefix, such as orders:read, also helps keep them apart.

Common errors and how to fix them

  • invalid_scope: the scope does not exist, is not attached to the client, or is not allowed. In Keycloak, add it to the client as an optional client scope.
  • Commas instead of spaces: the server sees one long unknown scope. Separate with spaces.
  • No ID token: openid is missing from the scope.
  • Claims you did not ask for: the scope is a default client scope in Keycloak.
  • Offline tokens not allowed for the user or client: the user lacks the offline_access role, or the client cannot request it.

For error codes in general, see the OAuth error lookup.

Security notes

Scopes are the main tool for least privilege. RFC 9700 recommends restricting access tokens to the minimum privileges an app needs, and to particular resource servers where possible.

A scope in a request is only a wish. The token’s scope claim, or the scope field in the token response, says what was actually granted. APIs must check the token, never the original request.

Avoid one catch-all scope that grants everything. If it leaks, everything leaks with it.

Best practices

  • Name API scopes by resource and action, such as orders:read and orders:write.
  • Request scopes when the user needs the feature, not all at sign-in.
  • Check the granted scope in the token response before you call an API.
  • Keep offline_access for real background work.
  • Document what each custom scope allows.
Questions

Frequently asked questions

Are OAuth scopes separated by commas or spaces?

By spaces. RFC 6749 section 3.3 defines the scope parameter as a space-separated list. In a URL the space becomes `%20`, and in a form body `+` or `%20`.

What does the openid scope do?

It makes the request an OpenID Connect request. The server then issues an ID token next to the access token. Without it, the request is plain OAuth.

Which scope do I need for the user’s email?

The `email` scope. It asks for the `email` and `email_verified` claims. Check `email_verified` before you trust the address.

What is the offline_access scope?

It asks for a refresh token that keeps working after the user’s session ends. OpenID Connect Core defines it in section 11. Use it only for work that runs while the user is away.

Why does Keycloak add scopes I did not request?

They are default client scopes for the client, which Keycloak applies to every token. Move a scope to optional on the client if you want to control it with the scope parameter.

Are scopes case-sensitive?

Yes. RFC 6749 treats scope tokens as case-sensitive strings. `Email` and `email` are different scopes.

Why do I get invalid_scope?

The scope is unknown to the server, not attached to the client, or not allowed for it. In Keycloak, check that the client scope exists and is assigned to the client as default or optional.

What is the difference between scopes and roles?

A scope limits what a client may do with a token. A role says what a user or service is allowed to do. In Keycloak, the `roles` client scope adds the user’s roles to the token, and APIs often check both.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.