← All articles

OAuth Scopes Must Match in APEX 26.2: Fix Custom Token Code Before You Upgrade

APEX 26.2 enforces OAuth token reuse and scope matching for REST web service calls. Custom PL/SQL that fetches a token for one scope and uses it for a call that needs another can now fail. Here is the failing pattern, the declarative fix, and how to find affected code.

AI-readable summary

Oracle APEX 26.2 enforces proper OAuth token reuse between calls and the scope matching rules for REST web service calls. Oracle's Changed Behavior example is custom code that requests a token by hand with APEX_WEB_SERVICE.MAKE_REST_REQUEST for one scope, sets it with the token-setting API and then makes a REST request that needs a different scope: APEX no longer uses that token. The release notes name the API SET_OAUTH_TOKEN, while the API reference documents APEX_WEB_SERVICE.OAUTH_SET_TOKEN. Oracle asks that the scopes of manually requested tokens match the scopes in the Web Credential and in the request, and recommends declarative OAuth authentication instead of custom code. MAKE_REST_REQUEST accepts p_credential_static_id, p_token_url and p_oauth_scope, and APEX uses or requests an access token with the given scope, separating multiple scopes with blanks. OAuth2 Client Credentials Web Credentials hold the scope, support Named Scopes as aliases, and get new tokens transparently when one expires. Oracle does not document whether matching is exact or by subset, so each call should be tested on 26.2 before production.

The short version

Most APEX upgrades break nothing in REST integrations. APEX 26.2 has one exception worth checking: custom OAuth token code.

  • The change. APEX 26.2 “now enforces proper token re-use between calls and abides by the scope matching rules”.
  • What fails. A token you requested by hand for one scope, set for later calls, and then used for a request that needs a different scope. APEX does not use it.
  • The fix. Make the scopes of hand-requested tokens match the Web Credential and the request, or better, let APEX get the token declaratively.
  • The audit. Search your code for the token-setting API and hand-built token requests before you upgrade.

What Oracle says changed

The Changed Behavior entry, “Increased Security When Using Scopes and Tokens for OAuth Authentication of REST Web Service Calls”, warns that calls made with “non-standard code to handle OAuth authentication” may fail. Oracle’s example has three steps:

  1. An OAuth token is requested by hand using APEX_WEB_SERVICE.MAKE_REST_REQUEST with a specific scope.
  2. The token is set by hand with the token-setting API.
  3. The actual REST request, which requires a different scope, is made.

“If the scope of the manually-requested token does not match the scope of the actual REST request, the APEX engine does not use it.” Oracle’s direction is clear: fix custom code “so that OAuth scopes of manually requested tokens match scopes defined in the Web Credential and scopes used for making the REST request”, and “use declarative OAuth authentication instead of custom code whenever possible.”

A naming note before you search: the release notes call the setter APEX_WEB_SERVICE.SET_OAUTH_TOKEN, but the API reference documents APEX_WEB_SERVICE.OAUTH_SET_TOKEN. Search for both spellings.

Matching diagram with hypothetical scopes. A token requested by hand for erp.read and set with OAUTH_SET_TOKEN is reused for a GET request with scope erp.read, but not for a POST request that needs erp.write. A token obtained through the ERP_CRED Web Credential for erp.write is used for the POST request. A note says APEX uses or requests an access token with the scope of the call
In 26.2 a token only serves calls whose scope matches. Hypothetical scopes and names.

The failing pattern

A hypothetical example of the kind of code Oracle describes. The token comes from a hand-built request for erp.read, and a later call needs erp.write:

-- Hypothetical: token requested by hand for one scope
l_token_json := apex_web_service.make_rest_request(
    p_url         => 'https://idp.example.com/oauth2/token',
    p_http_method => 'POST',
    p_body        => 'grant_type=client_credentials&scope=erp.read' );
    -- client authentication omitted

apex_web_service.oauth_set_token(
    p_token => json_value( l_token_json, '$.access_token' ) );

-- ... then used for a call that needs another scope
l_orders := apex_web_service.make_rest_request(
    p_url         => 'https://erp.example.com/api/orders',
    p_http_method => 'POST',
    p_body        => l_order_json,
    p_oauth_scope => 'erp.write' );

Under 26.2’s rules, the erp.read token is not used for the erp.write call. Code like this tends to live in old integration packages that nobody has touched since they worked.

The declarative fix

Let APEX request and reuse the token. A Web Credential of type OAuth2 Client Credentials holds the client ID, secret and scope, and MAKE_REST_REQUEST can reference it:

l_orders := apex_web_service.make_rest_request(
    p_url                  => 'https://erp.example.com/api/orders',
    p_http_method          => 'POST',
    p_body                 => l_order_json,
    p_credential_static_id => 'ERP_CRED',
    p_token_url            => 'https://idp.example.com/oauth2/token',
    p_oauth_scope          => 'erp.write' );

The documented behavior for p_oauth_scope: “APEX uses or requests an access token with the specified scope. Separate multiple scopes with blanks.” With OAuth2 Client Credentials, “If the access token is expired, APEX will transparently request a new one”, and a refresh token is used when the server issued one.

A code diff of a hypothetical fix. Removed lines request a token by hand from the token URL with scope erp.read and set it with oauth_set_token. The kept make_rest_request call to the orders endpoint gains p_credential_static_id ERP_CRED and p_token_url, and keeps p_oauth_scope erp.write. Notes beside the diff explain that APEX uses or requests a token with the call's scope, refreshes expired tokens, and that Named Scopes act as aliases
Delete the token plumbing and keep the call. APEX handles the token for the scope you ask for.

Scopes on the Web Credential

For OAuth2 Client Credentials, the Web Credential’s OAuth Scope is what APEX sends to the token server, in a request body of the form grant_type=client_credentials&scope={scope}. Three related details:

  • Named Scopes. “Named Scopes are alias names for OAuth Scopes. When invocations or token requests use the alias name, Oracle APEX will use the Scope Value instead.” Useful when the real scope strings are long or differ between environments.
  • Changing a scope. APEX_CREDENTIAL.SET_SCOPE changes the scope attribute, and “all existing tokens for the given credential are cleared”.
  • REST Data Sources. Declarative OAuth there means selecting the stored credential, and an Authentication Server remote server when authentication runs on a different server.

Oracle does not spell out whether scope matching is exact or allows a subset, so treat every distinct scope as its own token and test each call.

Finding affected code

  • Database packages. Search the source of your integration schemas, for example with select owner, name, type, line from all_source where upper(text) like '%OAUTH_SET_TOKEN%', and repeat for SET_OAUTH_TOKEN, OAUTH_GET_LAST_TOKEN and your token URLs.
  • Application code. Search processes, computations and dynamic actions in App Builder, or grep your APEXlang export.
  • Hand-built token requests. Look for grant_type=client_credentials in request bodies. Those are the calls Oracle’s example describes.
  • Staging. Run every integration once on 26.2 before production, including scheduled jobs and automations that only run at night.

Our APEX 26.2 upgrade checklist lists this change with the other behavior changes to test.

A short checklist

  • Code search done for both setter spellings and hand-built token requests
  • Each custom token’s scope compared with the Web Credential and the call
  • Custom token code replaced with p_credential_static_id and p_oauth_scope where possible
  • Multiple scopes separated with blanks
  • Named Scopes for long or environment-specific scope strings
  • Every integration exercised on a 26.2 staging instance, scheduled jobs included

Upgrades are where integrations quietly fail. For managed APEX environments with staged upgrades and support, see Cloud Hosting and the plans page.

Bottom line

APEX 26.2 stops reusing OAuth tokens across scopes, which is the secure behavior. The price is paid by old custom token code, and the fix is usually to delete it: reference a Web Credential, pass the scope, and let APEX manage tokens.

Search before you upgrade, not after the first nightly job fails.

FAQ

What changed for OAuth in APEX 26.2?

APEX now enforces proper token reuse between calls and the scope matching rules. A token requested by hand for one scope is not used for a request that needs a different scope.

Is the API SET_OAUTH_TOKEN or OAUTH_SET_TOKEN?

The API reference documents APEX_WEB_SERVICE.OAUTH_SET_TOKEN. The 26.2 release notes refer to SET_OAUTH_TOKEN. Search for both.

How should I pass several scopes?

Separate them with blanks, as the MAKE_REST_REQUEST and OAUTH_AUTHENTICATE_CREDENTIAL references describe.

Do I have to write token code at all?

Usually not. With an OAuth2 Client Credentials Web Credential, APEX requests the token, reuses it and gets a new one when it expires. Oracle recommends declarative OAuth whenever possible.

Does changing a Web Credential’s scope affect existing tokens?

Yes. APEX_CREDENTIAL.SET_SCOPE clears all existing tokens for that credential.

Are REST Data Sources affected?

Declarative REST Data Sources that use a stored credential are the path Oracle recommends. The change targets custom code that sets tokens by hand, but test every integration on 26.2 anyway.

Sources