Skip to main content

Generate tokens for organization applications

After you authorize applications to API resources and assign users to the appropriate roles, you can generate tokens to access the protected resources. Organization applications support the following OAuth 2.0 grant types to generate tokens and issue scopes.

Authorization code grant​

Use the following steps to generate an authorization code and exchange it for an access token.

https://localhost:9443/t/carbon.super/o/<ORG_ID>/oauth2/authorize
?response_type=code
&redirect_uri=<APPLICATION_REDIRECT_URI>
&client_id=<APPLICATION_CLIENT_ID>
&scope=<REQUIRED_SCOPE/S>

After executing the authorization request, WSO2 Identity Server prompts the user to sign in to the corresponding organization. After the user successfully signs in, the redirect URL of the application receives an authorization code. The application can exchange the authorization code for a token using the token endpoint.

curl --user <OAUTH_CLIENT_KEY>:<OAUTH_CLIENT_SECRET> -k
-d "grant_type=authorization_code&code=<AUTHORIZATION_CODE>&redirect_uri=<APPLICATION_REDIRECT_URI>"
-H "Content-Type: application/x-www-form-urlencoded"
https://localhost:9443/t/carbon.super/o/<ORG_ID>/oauth2/token
note
  • To obtain the refresh_token in the response, go to the Protocol tab of the application and enable the refresh token grant type.

  • The scope parameter returns only the scopes authorized to both the application and the user who requests authorization.

Password grant​

Use the following steps to use the user's credentials to get an access token.

curl --user <OAUTH_CLIENT_KEY>:<OAUTH_CLIENT_SECRET> -k
-d "grant_type=password&username=<USERNAME>&password=<PASSWORD>"
-H "Content-Type: application/x-www-form-urlencoded"
https://localhost:9443/t/carbon.super/o/<ORG_ID>/oauth2/token
note

If you need scopes in the response, add the scope parameter to the token request with the required scopes.

Client credentials grant​

Use the following steps to use the client credentials to get an access token.

curl --user <OAUTH_CLIENT_KEY>:<OAUTH_CLIENT_SECRET> -k
-d "grant_type=client_credentials"
-H "Content-Type: application/x-www-form-urlencoded"
https://localhost:9443/t/carbon.super/o/<ORG_ID>/oauth2/token

Token exchange grant​

Use the following request to exchange a subject token for an access token.

curl --user <OAUTH_CLIENT_KEY>:<OAUTH_CLIENT_SECRET> -k
-d "grant_type=urn:ietf:params:oauth:grant-type:token-exchange"
-d "requested_token_type=urn:ietf:params:oauth:token-type:access_token"
-d "subject_token_type=urn:ietf:params:oauth:token-type:jwt"
-d "subject_token=<subject_token>"
-d "scope=<desired scopes>"
-H "Content-Type: application/x-www-form-urlencoded"
https://localhost:9443/t/carbon.super/o/<ORG_ID>/oauth2/token
note

If you need scopes in the response, add the scope parameter to the token request with the required scopes.

Introspect tokens​

You can use the token introspection endpoint to validate access tokens issued for organization applications. The following example demonstrates how to introspect an access token.

curl --user <OAUTH_CLIENT_KEY>:<OAUTH_CLIENT_SECRET> -k
-d "token=<SUB_ORG_APP_TOKEN>"
-H "Content-Type: application/x-www-form-urlencoded"
https://localhost:9443/t/carbon.super/o/<ORG_ID>/oauth2/introspect

Revoke tokens​

The following example demonstrates how to revoke tokens issued for organization applications.

curl --user <OAUTH_CLIENT_KEY>:<OAUTH_CLIENT_SECRET> -k
-d "token=<SUB_ORG_APP_TOKEN>&token_type_hint=<TOKEN_TYPE>"
-H "Content-Type: application/x-www-form-urlencoded"
https://localhost:9443/t/carbon.super/o/<ORG_ID>/oauth2/revoke

Was this page helpful?