This article describes the process for obtaining user-scoped OpenID Connect (OIDC) or OAuth 2.0 tokens for Single-Page Applications (SPA), Web, or Native applications without using a browser. This technique is useful for unit, integration, or end-to-end testing.
- OpenID Connect (OIDC) and OAuth 2.0
- Single-Page Applications (SPA), Web, or Native apps using Implicit or Authorization Code Flow
- Implicit Flow or Authorization Code Flow
- Users with passwords in Okta and no Multi-Factor Authentication (MFA) requirements
- Okta Identity Engine (OIE)
- Okta Classic Engine
This method involves two or three API calls to Okta. The process starts by obtaining a sessionToken via primary authentication, which is then exchanged for tokens or an authorization code.
Prerequisites
- Use Postman Collections for formatting requests. Both the Authentication and OpenID Connect collections are required.
- Ensure the user is not prompted for MFA at the organization level.
- Note that external, federated, or social users cannot use this technique.
1. Complete Primary Authentication
Perform a POST request to the /authn endpoint to obtain a sessionToken.
- Submit a request to
POST https://<oktaDomain>/api/v1/authn. - Include the
usernameandpasswordin the JSON body. - Verify the response status is
SUCCESS. - Copy the
sessionTokenvalue from the response.
NOTE: If the status is not SUCCESS, refer to the Primary Authentication API documentation to complete the transaction.
2. Make an Authorize Request
Exchange the sessionToken for tokens or an authorization code. Use the built-in Org Authorization Server or a Custom Authorization Server.
- Submit a request to
GET https://<oktaDomain>/oauth2/v1/authorizeorGET https://<oktaDomain>/oauth2/<authorizationServerId>/authorize. - Provide the following query parameters:
client_id: The ID of the application.response_type: Set toid_token,token, orcode.response_mode: Set toform_post.sessionToken: The value obtained in Step 1.scope: Space-separated scopes (for example,openid profile).redirect_uri: A registered Sign-in redirect URI.nonceandstate: Arbitrary strings.
For Proof Key for Code Exchange (PKCE), include code_challenge and code_challenge_method.
Outcome
- Implicit Flow: If
response_typeisid_tokenortoken, the tokens appear in the HTML body of the response. Implicit Flow does not support Proof Key for Code Exchange (PKCE). - Authorization Code Flow: If
response_typeiscode, the response contains acodevalue. Proceed to Step 3.
3. Make a Token Request (Authorization Code Flow Only)
Exchange the authorization code for tokens.
- Submit a request to
POST https://<oktaDomain>/oauth2/v1/tokenorPOST https://<oktaDomain>/oauth2/<authorizationServerId>/token. - Set the Content-Type to
application/x-www-form-urlencoded. - Provide the
grant_type,code,redirect_uri,client_id, andcode_verifier(if using PKCE).
The tokens created can now be used to test the resource server or other integrations.
NOTE: This technique is intended for testing and debugging. Production requests to the /authorize endpoint should redirect the browser. Asynchronous JavaScript and XML (AJAX) cannot be used with the /authorize endpoint.
