OBLYTECH Logo
OBLYTECH Logo

ServiceNow Get OAuth Token with a REST Client

To get an OAuth token from a ServiceNow instance, register the application in the Application Registry for a client ID and client secret. Then send a form-encoded POST request to the instance’s token endpoint, oauth_token.do, with a grant type of password plus those credentials. The instance returns an access token.

The token endpoint and what it accepts

Token requests go to the oauth_token.do path on your own instance, which is the instance name followed by the instance domain and that path. The request is a POST, and the body must be sent as application/x-www-form-urlencoded. The token request uses the application/x-www-form-urlencoded content type, so check the body type first when a correct-looking request fails. For the wider concept of how OAuth settings are grouped on the platform, see OAuth profile explained.

In a REST client, that means choosing the form-urlencoded body option and adding each parameter as a key and value pair, rather than typing a JSON object into a raw body. For background on the platform APIs in general, see does ServiceNow have an API.

The password grant is the request this page walks through. The wider topic is covered under REST and SOAP integrations.

Parameter Used with Meaning
grant_type both, required Set to password
client_id both, required The ID generated for your application in the registry
client_secret both, required The shared secret between the instance and your application
username password grant The user account name
password password grant The password of that user account

Step 1: Register the application and get a client ID and secret

The token request cannot work until the instance knows the calling application. Registration is done in the Application Registry.

  1. Sign in with a user who holds the oauth_admin or admin role.
  2. Go to All, then System OAuth, then Application Registry, and select New.
  3. Choose the option to create an OAuth API endpoint for external clients.
  4. Fill in the form and select Submit.

When you submit, the instance creates the OAuth client record for the application. The client ID and client secret you need for the token request come from that record. Issued tokens are kept separately, as covered further down this page.

The fields that matter for a token request

The form has more fields than you need for a first token. These are the ones that shape what you get back:

  • Name is a unique label for the application that needs access.
  • Client ID is read-only and generated for you. This is the value you place in client_id.
  • Client Secret is the value for client_secret. The lock icon next to the field displays an existing secret.
  • Redirect URL is the callback address the authorization server returns to. Enter the full client address ending in /oauth_redirect.do.
  • Active switches the registry entry on.
  • Access Token Lifespan is the number of seconds an access token stays valid. The default is 1800 seconds, which is 30 minutes.
  • Refresh Token Lifespan is the number of seconds a refresh token stays valid. The default is 8640000 seconds, which is 100 days.
  • Enforce Token Restrictions limits tokens to APIs that are set to allow the authentication profile; access can be granted through an API access policy. It is unselected by default.
  • Token Format decides the structure of the generated token and the information inside it. When the format is JWT, the Subject Claim field applies.
  • Client Type offers Iframe Embedded, Integration as a User and Integration as a Service.

If you plan to read the secret into a client, copy it at registration time using the lock icon, and store it in a place meant for secrets rather than in a shared collection. For access design around these calls, see REST and SOAP security and access design.

Step 2: Request a token with the password grant

The password grant trades a user name and password, together with the application’s client ID and secret, for tokens. The steps below build the request. For the outbound direction, where the instance calls another system, see outbound REST message explained.

  1. Set the method to POST and the address to the oauth_token.do path on your instance.
  2. Choose the form-urlencoded body type.
  3. Add grant_type with the value password.
  4. Add client_id and client_secret from the registry record.
  5. Add username and password for the user the token should act as.
  6. Send the request.

A successful password grant returns an access token. The grant can also be switched off on the instance. When the glide.oauth.inbound.ropc.grant_type.disabled system property is set to true, the password grant cannot be used to grant OAuth 2 tokens, so a request that fails with correct credentials is worth checking against that property. The platform recommends the value true, and the official text notes that OAuth 2.1 has deprecated this grant.

Where issued tokens live and how to revoke them

Tokens issued by the instance, and tokens it receives from outside providers, are stored in the oauth_credential table.

There are two screens for looking at them. Under Self-Service, My Connected Apps shows the tokens created when you granted an application access to something on the instance. Under System OAuth, Manage Tokens shows all tokens, and only administrators can open it. Opening a token needs any user or the oauth_admin role, but the Manage Tokens module itself is limited to administrators.

To block a token, open it by its name and select Revoke access. The record also shows its scope and expiration date, so you can check those first.

A token can also be revoked with a request to the oauth_revoke_token.do path on the instance, with the token appended as a token query parameter. The request needs no authentication and marks the token expired.

Expired credentials do not pile up indefinitely. The Clean Expired OAuth Credentials record on the Schedule page controls clearing of expired credentials, using three system properties:

Property Purpose
com.snc.platform.security.oauth.is.active Turns the job on; the default is true
com.snc.platform.security.oauth.hours.expired.credential.is.kept Hours that expired credentials are retained
com.snc.platform.security.oauth.day.old.credential.is.kept Days that expired credentials are retained

Troubleshooting a token request that fails

Work through the causes in the order they are cheapest to rule out. For wider integration faults, see the troubleshooting guide for REST and SOAP integrations.

  1. Body type. Confirm the body is form-urlencoded and not JSON.
  2. Grant type spelling. The value must be exactly password.
  3. Client values. Confirm the client ID and secret match the registry record, and that the record is active.
  4. Credentials. For the password grant, confirm the user name and password match an account on this instance, and that the password grant is not switched off by the system property.

Frequently asked questions

What address do I send the token request to?

Send it to the oauth_token.do path on your own instance, as a POST.

Why does my request fail when the body looks correct?

Start with the body type, which should be form-urlencoded. Then check that the user name and password match an account on this instance, and that the password grant has not been switched off by the glide.oauth.inbound.ropc.grant_type.disabled system property.

How long does an access token last?

Thirty minutes by default, which is 1800 seconds. The Access Token Lifespan field on the registry record changes it.

How do I cancel a token I no longer want to be valid?

Open it under System OAuth, Manage Tokens, and select Revoke access. A request to the oauth_revoke_token.do path with the token attached also revokes it.