Authorization code grant workflow
ServiceNow® handles both authentication and API access by acting as the authorization and resource server. When single sign-on (SSO) is enabled, it redirects users to the configured IdP for authentication and issues tokens after successful login.
Before you begin
Role required: oauth_admin, mi_admin, admin
About this task
This topic collection provides information about how ServiceNow manages authentication and API access when acting as both the authorization server and the resource server. It describes the behavior when SSO is enabled, including redirection to the identity provider (IdP) for user authentication and the issuance of an authorization code by ServiceNow after successful authentication. The usage of authorization code ensures that ServiceNow retains control over token issuance and access to protected resources.
Procedure
-
Log in from the client application.
The user begins the login process from the client application interface.
-
Initiate the authorization request.
The client redirects the user to ServiceNow authorization endpoint to initiate the authorization request. The authorization request can be initiated either by including the
Client SecretorPKCE Code Challengein the request body, based on the client type- private or public. In the authorization request body, includeClient Secretfor private clients, andPKCE Code Challengefor public clients.- For Private clients
- Include the
Client Secretin the request body, while initiating the token request for private clients. Follow the procedure to initiate the authorization request usingClient Secretfor private clients. - Perform a GET request to the authorization endpoint with the following parameters:
Method: GET Endpoint: https://<servicenow_base_url>/oauth_auth.doTable 1. Authorization Request Parameters (Private Client-Client Secret) Parameter Required Description response_typeYes Set the value to codeto initiate the authorization code flow.client_idYes The unique identifier for your client application. Format: YOUR_CLIENT_ID
redirect_uriYes The URI to which ServiceNow sends the authorization code. Example: https://yourapp.com/callback
scopeYes A space-delimited list of requested scopes. Example:
incident_read incident_write.stateYes A client-generated value used to avoid Cross-Site Request Forgery (CSRF) attacks. The value is returned unchanged in the redirect URI, enabling the client to validate it - For Public clients
- Include the
PKCE Code Challengein the request body, while initiating the token request for public clients. Follow the procedure to initiate the authorization request usingPKCE Code Challengefor public clients. - Perform a GET request to the authorization endpoint with the following parameters:
Method: GET Endpoint: https://<servicenow_base_url>/oauth_auth.doTable 2. Authorization Request Parameters (Public Client - PKCE) Parameter Required Description response_typeYes Set the value to codeto initiate the authorization code flow.client_idYes The unique identifier for your client application. Format: YOUR_CLIENT_ID
redirect_uriYes The URI to which ServiceNow sends the authorization code. Example: https://yourapp.com/callback
code_challengeYes A base64url-encoded SHA-256 hash of the code verifier. This is used as part of the PKCE flow. code_challenge_methodYes Specifies the transformation method used for the code challenge. Set to S256.scopeYes A space-delimited list of requested scopes. Example:
incident_read incident_write.stateYes A client-generated value used to avoid CSRF attacks. The value is returned unchanged in the redirect URI, enabling the client to validate it. Note:Starting with the Madrid release, the system propertyglide.oauth.state.parameter.requiredmandates the use of thestateparameter in the OAuth requests. Thestateproperty is set totrueby default in the new instances, andoptionalin upgraded instances. In case of missingstateparameter, the authorization request fails and the following error is displayed:Missing State parameter in request.
-
Grant access consent to the client application.
Access the ServiceNow login page (or IdP, if SSO is enabled), and grant access consent to the client application.
-
ServiceNow (or IdP, if SSO is enabled) validates the credentials and ServiceNow returns an authorization code to the client.
After the successful authentication, the browser is redirected to the
redirect_uri, and the authorization code is included in the query string:https://yourapp.com/callback?code=AUTH_CODE&state=xyz123 -
The client exchanges the authorization code for an access token (and a refresh token, if it’s a private client) by making a call to ServiceNow’s token endpoint.
The authorization code for access token can be initiated either by including the
Client SecretorPKCE Code Challengein the request body, based on the client type- private or public. In the token request body, includeClient Secretfor private clients, andPKCE Code Challengefor public clients.- For Private clients
- Include the
Client Secretin the request body, while initiating the token request for private clients. Follow the procedure to initiate the token request usingClient Secretfor private clients. - In case of private clients, the client sends a
POSTrequest to token endpoint with the following parameters:Method: POST Endpoint: https://<servicenow_base_url>/oauth_token.do Headers: Content-Type: application/x-www-form-urlencodedTable 3. Token Request Parameters (Private Client-Client Secret) Parameter Required Description grant_typeYes Set the value to authorization_codeto exchange the code for a token.codeYes The authorization code received from the authorization endpoint. redirect_uriYes The URI used in the initial authorization request. Example: https://yourapp.com/callback
client_idYes The unique identifier for your client application. client_secretYes The client’s secret used to authenticate with the token endpoint. stateYes A client-generated value used to help prevent CSRF attacks. The value is returned unchanged in the redirect URI, enabling the client to validate it. - For Public clients
- Include the
PKCE Code Challengein the request body, while initiating the token request for public clients. Follow the procedure to initiate the authorization request usingPKCE Code Challengefor public clients. - The client sends a POST request to token endpoint with the following parameters:
Method: POST Endpoint: https://<servicenow_base_url>/oauth_token.do Headers: Content-Type: application/x-www-form-urlencodedTable 4. Token Request Parameters (Public Client-PKCE) Parameter Required Description grant_typeYes Set the value to authorization_codeto exchange the code for a token.codeYes The authorization code received from the authorization endpoint. redirect_uriYes The URI used in the initial authorization request. Example: https://yourapp.com/callback
client_idYes The unique identifier for your client application. code_verifierYes The original string used to generate the PKCE code_challenge.stateYes A client-generated value used to help prevent CSRF attacks. The value is returned unchanged in the redirect URI, enabling the client to validate it.
-
Access the ServiceNow APIs with the access token.
- Example:
- Make a GET request to the APIs using the access token. Include the access token in the
Authorizationheader.
Method: GET End Point: https://<servicenow_base_url>/api/now/incident Authorization: Bearer YOUR_ACCESS_TOKEN -
Renew the access token if it has expired.
Make a POST request to refresh the access token (private clients only) with the following parameters:
Method: POST Endpoint: https://<servicenow_base_url>/oauth_token.do Headers: Content-Type: application/x-www-form-urlencodedTable 5. Refresh Token Request Parameters (Private Client) Parameter Required Description grant_typeYes Set the value to refresh_tokento request a new access token.refresh_tokenYes The refresh token previously issued by the token endpoint. client_idYes The unique identifier for your client application. client_secretYes The client secret used to authenticate with the token endpoint.