Join us Dec. 16 for The Community Hour, our first global virtual User Group event!

How can we programmatically create an inbound OAuth Client Credentials registry from a scoped app

ajayshrivas
Tera Contributor

Hi Community,

We are developing a scoped ServiceNow application and need to automate the setup of an inbound OAuth 2.0 Client Credentials integration as part of a setup wizard.

 

Requirement

Our application should automatically perform the following steps:

  1. Create a dedicated machine-identity user in sys_user.

  2. Assign the itil role through sys_user_has_role.

  3. Create an inbound OAuth 2.0 Client Credentials registry.

  4. Set the integration user as the OAuth Application User.

  5. Generate the Client ID and Client Secret.

  6. Associate an existing REST API/Table API authentication scope with the OAuth client.

Some of the points we done such as create user in sys_user table and assign role to that user. 

However, when we try to create an OAuth registry using GlideRecord from our scoped application, ServiceNow blocks the operation.

 

Error encounter - "has been refused due to the api's cross-scope access policy."

Can you please suggest me any alternative way and how can i achieve this, is this possible ? 

1 ACCEPTED SOLUTION

Hi @ajayshrivas 

Glad Option 2 unblocked you, and thanks for reporting back on Option 1. That result tells us the protection on oauth_entity is at the table level, so no cross-scope privilege record will ever be enough, which is useful for anyone else reading this.

 

On packaging: your instinct is right, and the constraint is unfortunately hard. Global-scope applications and artifacts are not eligible for the ServiceNow Store — a long-standing rule that still applies today (see the Understanding Application Scope whitepaper here on the Community, and the Store Publisher Portal knowledge base for the current certification requirements). So the global script include can't ship inside your Store app, and a companion global app can't be listed either. It's worth saying why before looking at alternatives: a Store app that silently mints an inbound OAuth credential on install is exactly what app security review is designed to question. Customers' security teams want a human consciously creating credentials. So I'd reframe the goal from "no admin involvement" to "single app, no separate deployment, one admin action", which is achievable.

 

Pattern A: One-click creation under the installing admin's own session

Your setup wizard already runs in front of an admin. Instead of creating the registry from server-side scoped code (blocked), have the wizard's UI make a client-side REST Table API call to create the oauth_entity record from the admin's browser session. That request isn't scoped-app server code, so the cross-scope policy doesn't apply to it; it runs under the admin's own access, with ACLs enforced as normal. From the customer's point of view it's still automatic: everything ships in your one scoped app, and the admin clicks one button in your wizard.

Three caveats. First, test it properly on a vendor instance: the table's ACLs and web-service accessibility decide whether it works, and that's a ten-minute check. Second, handle the session token correctly for the POST, the same way any UI page calling the Table API does. Third, and most important, document this behaviour explicitly in your certification submission: what the button creates, under whose authority, and why. Transparency is what separates an acceptable pattern from a red flag in review.

 

Pattern B: Guided Setup step

Ship a guided setup in your app where one step sends the admin to System OAuth > Application Registry with clear instructions (you can link them straight to the new-record form), then a verification step where they paste the resulting Client ID back into your app's configuration. More clicks than Pattern A, but it's the pattern reviewers see most often, it works on every release, and some security-conscious customers actually prefer it.

 

My recommendation

Build Pattern B as the baseline, and add Pattern A's one-click option on top if your testing confirms it works cleanly. Either way, keep the rest of your wizard exactly as you have it (user creation, role, validation), and in both patterns design the client secret flow so it's shown once to the admin and never stored by your app.

 

One more thing worth doing: raise this scenario with the Store certification team early, before you build too much around either pattern. They see this requirement regularly and will tell you what the current review stance is.

View solution in original post

4 REPLIES 4

Sreeram Nair
Kilo Sage

Hi @ajayshrivas 

 

You're hitting that error by design. The OAuth registry tables (oauth_entity and its related tables) live in global scope and are protected by a restrictive cross-scope access policy, because OAuth registries are security records. A scoped app isn't allowed to create them directly with GlideRecord, so this isn't something a different API call in the same scope will get around. The good news: your steps 1, 2, 4, 5 and 6 are all achievable; it's step 3 that needs one of the patterns below.

 

Option 1: Try the cross-scope privilege first (config only)

When your scoped script attempts the operation, ServiceNow logs the attempt under System Applications > Application Cross-Scope Access (sys_scope_privilege). An admin can open the record for oauth_entity create/write from your scope and set Status to Allowed, then retry. Whether this works depends on how the table's own scope protections are set. If the table forbids create from other scopes at the dictionary level, no privilege record can override it and you'll need Option 2. It's a five-minute test, so do it first on a dev instance.

 

Option 2: A global helper Script Include (the reliable pattern)

Create a small Script Include in Global scope with "Accessible from: All application scopes", exposing one narrow method, for example createClientCredentialsRegistry(name, appUserSysId). Because the script include executes in global scope, its writes to oauth_entity are permitted. Your scoped setup wizard calls it and gets back the sys_id.

Inside that method:

  1. Insert into oauth_entity with the type set for an OAuth client. Leave Client ID and Client Secret empty; the platform generates both on insert.
  2. Set the OAuth Application User field to the machine-identity user your wizard created. That field is what makes the Client Credentials grant work.
  3. Read back client_id. The secret is stored encrypted; design your wizard to reveal it once to the installing admin rather than persisting it anywhere.
  4. Create the scope association your APIs need, which is your step 6. The pieces, from the REST API Auth Scope plugin (com.glide.rest.auth.scope): the auth scope itself is an Authentication Scope record (sys_auth_scope); the link to a REST API is a REST API Auth Scope record (sys_api_access_scope), which names the API, path, version and method coverage; and the link to your client is made on the OAuth entity, via its OAuth Entity Scopes related list referencing the auth scope. A new OAuth entity has no auth scope by default. One behaviour to plan for: once an auth scope is linked to a REST API, existing tokens from clients without that scope stop working against that API, so your wizard should link the scope to the client before (or in the same step as) protecting the API.

A tip for building this: create one registry manually in the UI exactly as you want the wizard's output to look, then open each related list and note the tables and references involved on your release. The profile-level tables (oauth_entity_profile and its scope link) vary in how they're wired across releases, so copy what your own instance produces rather than hard-coding a chain from a blog post.

Keep the method narrow and validate its inputs, since you're deliberately opening a door from scoped code into a security table. Don't expose a generic "insert into any global table" helper.

 

Packaging consideration

If this app is only for your own instances, the global script include just travels in an update set or a second small global app, and you're done. If it's heading to the ServiceNow Store, be aware that global-scope artifacts and anything touching security tables get extra scrutiny in app review. The common alternative for Store apps is to make registry creation a documented post-install step in a guided setup, run by the customer's admin, rather than fully automatic. Customers' security teams often prefer that anyway, because it means an admin consciously creates the credential.

 

Two security suggestions for the wizard itself

  1. On the machine-identity user, tick "Web service access only" so the account can't log in interactively.
  2. Reconsider granting itil. For an inbound integration, a custom role with exactly the table access your API needs is safer, and it also keeps the account's licence classification clean. If the integration only ever hits your scoped tables, itil is almost certainly more than it needs.

 

One more check: the Client Credentials grant for inbound OAuth is controlled by the system property glide.oauth.inbound.client.credential.grant_type.enabled on some releases (it's enabled by default on recent ones). If your registry looks right but token requests fail, check that property for your release.

 

Hope that unblocks you. If you try Option 1, I'd be interested to hear whether the privilege record was enough on your release, since that's the quickest path when it works.

Thank you so much for your response, Sreeram.

 

I tried Option 1, but ServiceNow is blocking the operation due to cross-scope security restrictions. However, your Option 2 suggestion was really helpful, and I was able to successfully create a record in the oauth entity table using the Global Script Include approach.

 

I have one additional concern regarding deployment. Since the helper Script Include resides in the Global scope, it may not be captured along with the scoped application's XML or Update Set. Our requirement is to keep the entire implementation within a single scoped application so that we can publish and distribute it through the ServiceNow Store without requiring customers to deploy or configure anything separately.

 

Could you please suggest a recommended approach to achieve this while keeping the implementation compatible with ServiceNow Store requirements?

Thanks again for your guidance!

Hi @ajayshrivas 

Glad Option 2 unblocked you, and thanks for reporting back on Option 1. That result tells us the protection on oauth_entity is at the table level, so no cross-scope privilege record will ever be enough, which is useful for anyone else reading this.

 

On packaging: your instinct is right, and the constraint is unfortunately hard. Global-scope applications and artifacts are not eligible for the ServiceNow Store — a long-standing rule that still applies today (see the Understanding Application Scope whitepaper here on the Community, and the Store Publisher Portal knowledge base for the current certification requirements). So the global script include can't ship inside your Store app, and a companion global app can't be listed either. It's worth saying why before looking at alternatives: a Store app that silently mints an inbound OAuth credential on install is exactly what app security review is designed to question. Customers' security teams want a human consciously creating credentials. So I'd reframe the goal from "no admin involvement" to "single app, no separate deployment, one admin action", which is achievable.

 

Pattern A: One-click creation under the installing admin's own session

Your setup wizard already runs in front of an admin. Instead of creating the registry from server-side scoped code (blocked), have the wizard's UI make a client-side REST Table API call to create the oauth_entity record from the admin's browser session. That request isn't scoped-app server code, so the cross-scope policy doesn't apply to it; it runs under the admin's own access, with ACLs enforced as normal. From the customer's point of view it's still automatic: everything ships in your one scoped app, and the admin clicks one button in your wizard.

Three caveats. First, test it properly on a vendor instance: the table's ACLs and web-service accessibility decide whether it works, and that's a ten-minute check. Second, handle the session token correctly for the POST, the same way any UI page calling the Table API does. Third, and most important, document this behaviour explicitly in your certification submission: what the button creates, under whose authority, and why. Transparency is what separates an acceptable pattern from a red flag in review.

 

Pattern B: Guided Setup step

Ship a guided setup in your app where one step sends the admin to System OAuth > Application Registry with clear instructions (you can link them straight to the new-record form), then a verification step where they paste the resulting Client ID back into your app's configuration. More clicks than Pattern A, but it's the pattern reviewers see most often, it works on every release, and some security-conscious customers actually prefer it.

 

My recommendation

Build Pattern B as the baseline, and add Pattern A's one-click option on top if your testing confirms it works cleanly. Either way, keep the rest of your wizard exactly as you have it (user creation, role, validation), and in both patterns design the client secret flow so it's shown once to the admin and never stored by your app.

 

One more thing worth doing: raise this scenario with the Store certification team early, before you build too much around either pattern. They see this requirement regularly and will tell you what the current review stance is.

Thankyou @Sreeram Nair for your suggestions, Its really helpful for me. 
Through client-side REST Table API call, we achieved our objective to create oauth registry, user creation and other record creation.

I have one more concern is that, not sure is this the correct way as per servicenow store application publication or not? and security concerns.