Showing posts with label ServiceNow Integrations. Show all posts
Showing posts with label ServiceNow Integrations. Show all posts

Sunday, December 07, 2025

OAuth Grant Types in ServiceNow — A Practical Guide for External Integrations

OAuth Grant Types in ServiceNow — A Practical Guide for External Integrations

Choosing the wrong OAuth grant type is one of the most common causes of failed integrations in ServiceNow. Symptoms include:

  • Session expired messages

  • Invalid CSRF token errors

  • Unexpected redirects to login pages

  • Token endpoint failures

  • API calls succeeding in one instance but failing in another

This article explains the four main OAuth grant types in ServiceNow, what each one is designed for, and which external tools should use which flow.


Why Grant Type Matters

When an external tool requests an access token, ServiceNow evaluates:

  • How the client identifies itself

  • Whether a user’s credentials are required

  • Whether a redirect URL is needed

  • Whether the request is browser-based or server-to-server

If the wrong grant type is used, ServiceNow may:

  • reject the request

  • redirect to a login page

  • return HTML instead of JSON

  • treat the call as UI traffic and enforce CSRF protection

This is why integrations like AWS Glue, which expect simple JSON token responses, fail when the grant type is incorrect.


The Four Main OAuth Grant Types in ServiceNow

1. Client Credentials (Most Common for Integrations)

Best for:

  • Server-to-server integrations

  • Tools that authenticate using only client ID + secret

  • Automated systems with no user interaction

Examples:
AWS Glue, MuleSoft, Boomi, custom API clients, backend services.

How it works:
The integration uses:

  • Client ID

  • Client Secret

No user login, no redirect, no password.

Why it’s recommended:

  • Simple

  • Secure

  • Stable

  • No user dependency

  • No browser interaction

  • Works consistently across environments

Common error when not used:
Tools expecting this flow may receive “session expired” or CSRF errors when configured incorrectly as a different grant type.


2. Resource Owner Password Credentials (ROPC)

Best for:
Legacy applications that authenticate using a specific user’s username and password.

Why it’s risky:

  • Requires storing a user’s password in an external system

  • Breaks if password expires or the user is locked

  • Not allowed in many organizations due to security policies

Why integrations fail with ROPC:
If the integration expects to authenticate using just client ID + secret (like Glue), but the OAuth registry uses ROPC:

  • ServiceNow rejects the request

  • Redirects to the login page

  • The external system receives HTML instead of a token

  • This triggers a misleading CSRF/session-expired error

Use only if:
You absolutely must impersonate a user and have no modern integration path.


3. Authorization Code Grant (with Redirect)

Best for:

  • Web apps where a human signs in

  • Browser-driven flows

  • Portals that require explicit user consent

Examples:
Custom web apps, portals, internal tools with UI login pages.

Characteristics:

  • Requires a redirect URL

  • Requires a human user

  • Longer setup

  • Not suitable for automated systems

Why integrations fail with this:
If an automated tool uses this grant type, ServiceNow tries to redirect it to a login page → causing “session expired.”


4. Refresh Token Grant

Best for:
Extending an existing session without re-authenticating.

Works only after:
The Authorization Code or Resource Owner flow has already created a token.

Not used alone — it's always a secondary mechanism.


Grant Type Comparison Table

Grant TypeUse CaseRequires User LoginWorks for AutomationRecommended for APIs
Client CredentialsServer-to-serverNoYes⭐ Yes
Resource Owner PasswordLegacy user-based authYesNo❌ No
Authorization CodeBrowser/web loginYesNo❌ No
Refresh TokenExtend existing tokenSometimesYesConditional

How to Check the Grant Type in ServiceNow

Go to:
System OAuth → Application Registry → [Your OAuth App]

Look for the field:
Grant type

Allowed values will show one or more of:

  • Client Credentials

  • Resource Owner Password Credentials

  • Authorization Code

  • Refresh Token

If the wrong type is selected, update it and re-test.


Why Grant Type Issues Only Affect One Instance

A common scenario:

  • Integration works in DEV

  • Works in PROD

  • ❌ Fails in TEST

This occurs because:

  • Client ID is different

  • Grant type is different

  • Scopes differ

  • Redirect URL differs

  • The integration user is locked only in one instance

Even one field mismatch causes OAuth to break.


Best Practices for Reliable Integrations

✔ Always use Client Credentials for non-user-driven systems

Simple, predictable, secure.

✔ Avoid Resource Owner Password

Too risky and breaks easily.

✔ Disable unused grant types

Reduces confusion and improves security.

✔ Standardize OAuth registry across all instances

Use a controlled update set or automation job.

✔ Clearly document client ID + secret per environment

Prevents accidental cross-environment use.


Final Thoughts

Most integration failures in ServiceNow involving OAuth come down to one root cause: incorrect grant type selection. By understanding how each grant type works and matching it to the correct use case, teams can avoid authentication errors, remove support overhead, and ensure stable integrations across all environments.

Understanding ServiceNow CSRF Token Errors in Integrations (AWS Glue Example)

Understanding ServiceNow CSRF Token Errors in Integrations

CSRF protection in ServiceNow is designed for browser traffic—not integrations. When an external tool like AWS Glue receives “Invalid CSRF token” or “Your session has expired”, it often indicates a deeper issue involving authentication flow, OAuth configuration, or an unexpected redirect.

The tricky part?
The integration may work in other ServiceNow instances, but fail in one specific environment, confusing both teams.

This article explains why this happens and how to diagnose and fix it quickly.


What Causes CSRF Token Errors in Integrations?

A ServiceNow CSRF error typically appears only in two situations:

1. The incoming request is treated like a browser session

If ServiceNow mistakenly thinks the external system is acting like a browser (UI request), it enforces CSRF token validation.

2. The authentication flow redirects incorrectly

If OAuth fails and redirects to a login page, the external system receives HTML instead of JSON, and ServiceNow displays “session expired."

Integrations should never see a CSRF token error under normal OAuth conditions.


Why AWS Glue Might Trigger a CSRF Error

AWS Glue generally integrates with ServiceNow by performing:

  • OAuth authentication

  • REST API requests

  • Data reads/writes

If Glue sees:

  • “Invalid CSRF token”

  • “Your session has expired. Click here to login again”

…it means ServiceNow did not accept the incoming credentials as API traffic.

Below are the real reasons this happens.


1. Incorrect OAuth Grant Type

This is the #1 cause.

Most integrations use:

Client Credentials (client_id + client_secret)

But some teams accidentally configure:

Resource Owner Password Credentials (ROPC)
This requires a username + password — not suitable for Glue.

If Glue sends only client ID + secret, and your instance expects the other grant type, ServiceNow redirects to the login page → triggering the CSRF/session expired error.


2. OAuth Client ID Mismatch

Each ServiceNow instance typically has a different client ID for the same application registry.

If AWS Glue is configured with:

  • correct client ID for DEV

  • correct client ID for PROD

  • ❌ but wrong or old client ID for TEST

…the authentication silently fails and ServiceNow falls back to the login URL.

This results in a misleading CSRF message.


3. The “External Logout Redirect” URL Is Misleading

Identity Provider configuration sometimes contains:

External Logout Redirect: <ServiceNow session expired page>

When OAuth authentication fails, the system mistakenly redirects to this URL.

Glue receives:
HTML page saying “Your session has expired”
→ treated internally as a CSRF failure.

This confuses both sides because the message looks unrelated.


4. The Integration User Account Is Locked

If the integration uses Resource Owner flow (even unintentionally):

  • the actual ServiceNow user behind it must not be locked

  • If the “xyz” user that set up Glue is locked → authentication fails → login redirect happens → CSRF-tinted error

This explains scenarios like:
✔ Works in other instances
❌ Fails in one where xyz is locked


5. Instance-Specific Redirect or SSO Policy

Sometimes, one instance may have:

  • a stricter SSO policy

  • more restrictive login rules

  • IP filtering that triggers a redirect

These subtle differences can cause OAuth to misbehave in only one environment.


6. ServiceNow Treating the API Endpoint as UI

If the wrong endpoint is used (for example, a UI page instead of /api), ServiceNow expects CSRF tokens and blocks the call.

Even a missing /now prefix can cause this.


7. Auth Scope Confusion

If the OAuth app registry defines scopes like:

  • user_account

  • table.read

  • global

…but Glue is configured to request a scope that is not registered in that environment, OAuth rejects the request and redirects to login.

Thus → “session expired.”


How to Diagnose the Issue

Here is a clean, systematic approach.

Step 1 — Check the OAuth transaction logs

Go to:
sys_oauth_credential → “OAuth Transaction Logs”

Failed transactions clearly show:

  • wrong grant type

  • invalid client ID

  • unexpected redirect

  • authentication errors


Step 2 — Impersonate the Integration User

If ROPC is used:

  • impersonate the user configured in Glue

  • check whether it is locked

  • check whether it is active

  • check if password expired


Step 3 — Compare OAuth App Registry Across Instances

Check these fields:

  • OAuth name

  • Client ID

  • Client Secret

  • Token URL

  • Redirect URL

  • Grant Types

  • Scopes

When the integration works in one instance and not another, at least one of these fields differs.


Step 4 — Ask Glue Team for Their Logs

Glue logs will show:

  • whether token URL returned HTML

  • the redirect URL

  • the HTTP error code

If they provide a login page instead of JSON → OAuth is broken.


Step 5 — Ensure Glue Is Calling /token and /api Endpoints

The valid pattern is usually:

https://<instance>.service-now.com/oauth_token.do

and APIs under:

/api/now/table/

Anything else → browser rules applied → CSRF triggered.


Final Thoughts

CSRF token errors in integrations can be misleading, but the root cause is almost always related to OAuth flow issues—not actual CSRF protection.

By checking:

  • grant type

  • OAuth client ID

  • scopes

  • redirect behavior

  • integration user status

  • endpoint URLs

You can resolve the issue in minutes and bring the integration back online.