Authorization code grant workflow
The OAuth authorization code grant is a secure and widely used flow for web, mobile, or desktop apps that access user data with user consent. It supports both private clients (using a client secret), and public clients (using PKCE).
Before you begin
Role required: oauth_admin, mi_admin, admin
About this task
This topic collection provides information on how a client application can use the Authorization code grant flow to obtain a token from ServiceNow and make API calls with that token. Private clients use client secret, while public clients use PKCE code challenge.
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 the ServiceNow authorization endpoint. The method of initiating the authorization request depends on the type of client: Public or Private.
- Public Clients
-
Public clients (Example: Mobile or Single-page applications) cannot securely store a client secret. Therefore, they must use Proof Key for Code Exchange (PKCE) to enhance security.
- In the authorization request, include the PKCE code challenge and specify the code challenge method.
- During the token request, the client must send the code verifier to validate the authorization code.
- 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 (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.scopeOptional 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. - Private Clients
-
Private clients (Example: Server-side applications) can securely store a client secret and do not require PKCE.
- The authorization request is initiated by redirecting the user to the authorization endpoint. No client secret or PKCE code challenge is required in this step.
- During the token request, the client includes the client secret along with the authorization code to obtain the access token.
- 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 (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
scopeOptional 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.
-
Log in and grant access consent to the client application.
Log in to ServiceNow (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 -
Initiate the authorization request.
The client redirects the user to the ServiceNow authorization endpoint for an access token. The method of initiating the authorization request depends on the type of client: Public or Private.
- Public Clients
-
Public clients (Example: Mobile or Single-page applications) cannot securely store a client secret. Therefore, they must use Proof Key for Code Exchange (PKCE) to enhance security.
- In the authorization request, include the PKCE code challenge and specify the code challenge method.
- During the token request, the client must send the code verifier to validate the authorization code.
- 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 3. 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. - Private Clients
-
Private clients (Example: Server-side applications) can securely store a client secret and do not require PKCE.
- The authorization request is initiated by redirecting the user to the authorization endpoint. No client secret or PKCE code challenge is required in this step.
- During the token request, the client includes the client secret along with the authorization code to obtain the access token.
- Perform a POST request to the authorization 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 (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.
-
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.