GenerateToken
Generates an access token for an application in a specified IDaaS EIAM instance by using credential information.
Operation description
Supported grant types: authorization code, device flow, refresh token, client credentials, and password.
1. Authorization code grant (Authorization Code)
Applicable scenario: Standard OAuth 2.0 authorization code flow, suitable for web applications with frontend interaction. Call example:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code={authorization_code}
&redirect_uri={redirect_uri}
&client_id={client_id}
&client_secret={client_secret}
Parameter description:
● code: The authorization code obtained from the authorization endpoint.
● redirect_uri: Must be the same as the redirect_uri used when obtaining the code.
1.1 Authorization code grant for public clients
Applicable scenario: Suitable for applications that cannot securely store secrets, such as SPAs and native applications. In this flow, client_secret is not required, but the PKCE mechanism must be used. Call example:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code={authorization_code}
&redirect_uri={redirect_uri}
&client_id={client_id}
&code_verifier={code_verifier}
Parameter description:
● code_verifier: The verification key in the PKCE mechanism. It is generated by the client when initiating the authorization request and is used to derive the code_challenge. When exchanging the token, this value must be submitted as is and must match the value used to generate the code_challenge.
Java example for generating code_verifier and code_challenge:
String codeVerifier = Base64.getUrlEncoder().withoutPadding().encodeToString(new SecureRandom().generateSeed(43));
String codeChallenge = Base64.getUrlEncoder().withoutPadding().encodeToString(java.security.MessageDigest.getInstance("SHA-256").digest(codeVerifier.getBytes()));
2. Device flow
Applicable scenario: Suitable for input-constrained devices such as TVs and IoT devices. Call example:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:device_code
&device_code={device_code}
&client_id={client_id}
&client_secret={client_secret}
Obtaining the device code: You must first call /oauth2/device/code to obtain the device_code and user_code.
2.1 Device flow for public clients
Applicable scenario: Suitable for public clients where interactive logon is inconvenient. Call example:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:device_code
&device_code={device_code}
&client_id={client_id}
3. Refresh token
Applicable scenario: Use a refresh_token to obtain a new access_token. Call example:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token
&refresh_token={refresh_token}
&client_id={client_id}
&client_secret={client_secret}
4. Client credentials grant
Applicable scenario: Server-to-server authentication without user involvement. Call example:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&client_id={client_id}
&client_secret={client_secret}
&scope={scope}
5. Password grant
Applicable scenario: Traditional username and password authentication (use with caution). Call example:
POST /v2/{instanceId}/{applicationId}/oauth2/token
Content-Type: application/x-www-form-urlencoded
grant_type=password
&username={username}
&password={password}
&client_id={client_id}
&scope={scope}
Try it now
Test
RAM authorization
Request syntax
POST /v2/{instanceId}/{applicationId}/oauth2/token HTTP/1.1
Path Parameters
|
Parameter |
Type |
Required |
Description |
Example |
| instanceId |
string |
Yes |
The instance ID. |
idaas_ue2jvisn35ea5lmthk267xxxxx |
| applicationId |
string |
Yes |
The application ID. |
app_mkv7rgt4d7i4u7zqtzev2mxxxx |
Request parameters
|
Parameter |
Type |
Required |
Description |
Example |
| client_id |
string |
No |
The client ID. |
app_mkv7rgt4d7i4u7zqtzev2mxxxx |
| client_secret |
string |
No |
The client secret. This value is required when grant_type is client_credentials and the client_secret_post method is used. |
CSEHDcHcrUKHw1CuxkJEHPveWRXBGqVqRsxxxx |
| grant_type |
string |
Yes |
The grant type. Valid values:
Valid values:
|
client_credentials |
| code |
string |
No |
The authorization code. This parameter is required when grant_type is authorization_code. |
CO541xY59EsKniV2wvWDXZ4jiKxxxxx |
| username |
string |
No |
The username. This parameter is required in the password grant mode. |
uesrname_001 |
| password |
string |
No |
The username. This parameter is required in the password grant mode. |
ExamplePassword123! |
| device_code |
string |
No |
The device code. This parameter is required when grant_type is urn:ietf:params:oauth:grant-type:device_code. |
DCxxxxxx |
| redirect_uri |
string |
No |
The redirect URI. This parameter is required when grant_type is authorization_code. It must be the same as the redirect URI used in the authorization code request. |
https://example.com/xxxxx |
| refresh_token |
string |
No |
The refresh token. This parameter is required when grant_type is refresh_token. |
ATxxx |
| code_verifier |
string |
No |
The code verifier used when the PKCE mechanism is enabled in the authorization code grant mode. |
dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk |
| exclusive_tag |
string |
No |
The excluded tag. |
ATxxx |
| scope |
string |
No |
The scope. This parameter is optional. Multiple values are supported and must be separated by spaces. Valid values:
|
openid |
Response elements
|
Element |
Type |
Description |
Example |
|
object |
The response result. |
||
| token_type |
string |
The token type. Valid values:
Valid values:
|
Bearer |
| access_token |
string |
The access token. |
ATxxx |
| refresh_token |
string |
The refresh token. |
RTxxx |
| expires_in |
integer |
The validity period, in seconds. |
1200 |
| expires_at |
integer |
The expiration time. The value is a UNIX timestamp in seconds. |
1653288641 |
| id_token |
string |
The ID token. |
eyJraWQiOiJLRVlLZ0Iyxxxxx |
Examples
Success response
JSON format
{
"token_type": "Bearer",
"access_token": "ATxxx",
"refresh_token": "RTxxx",
"expires_in": 1200,
"expires_at": 1653288641,
"id_token": "xxxxx"
}
Error codes
See Error Codes for a complete list.
Release notes
See Release Notes for a complete list.