The token endpoint supports three grant types for obtaining access tokens:
authorization_code - Exchange an authorization code for tokens
refresh_token - Refresh an expired access token
urn:ietf:params:oauth:grant-type:token-exchange - Exchange a token for delegated access
Authorization Code Grant
Exchange an authorization code for access tokens. Supports both PKCE (public clients) and client secret authentication (confidential clients).
Request Body
Must be authorization_code
The authorization code received from the authorization endpoint
The redirect URI used in the authorization request. Must match exactly.
Your OAuth application’s client ID
Your OAuth application’s client secret (for confidential clients)
PKCE code verifier (for public clients using PKCE)
Response
Opaque access token for API requests
JWT access token containing claims
Token lifetime in seconds (default: 3600)
Granted scopes (space-separated)
OpenID Connect ID token (if openid scope was granted)
Refresh token (if offline_access scope was granted)
User identity information
Ave user ID (if user_id scope was granted and allowed)
Encrypted app key for E2EE applications
Example with PKCE (Public Client)
import { exchangeCode } from '@ave-id/sdk' ;
const tokens = await exchangeCode (
{
clientId: 'your_client_id' ,
redirectUri: 'https://yourapp.com/callback' ,
},
{
code: authorizationCode ,
codeVerifier: codeVerifier ,
}
);
Example with Client Secret (Confidential Client)
import { exchangeCodeServer } from '@ave-id/sdk/server' ;
const tokens = await exchangeCodeServer (
{
clientId: 'your_client_id' ,
clientSecret: 'your_client_secret' ,
redirectUri: 'https://yourapp.com/callback' ,
},
{
code: authorizationCode ,
}
);
Refresh Token Grant
Refresh an expired access token using a refresh token. Implements token rotation for security.
Request Body
The refresh token received from a previous token response
Your OAuth application’s client ID
Your OAuth application’s client secret (for confidential clients)
Response
Returns the same structure as the authorization code grant, including a new rotated refresh token.
New refresh token (the old one is revoked)
Token lifetime in seconds
Ave user ID (if user_id scope was granted)
Example
import { refreshToken } from '@ave-id/sdk' ;
const tokens = await refreshToken (
{
clientId: 'your_client_id' ,
redirectUri: 'https://yourapp.com/callback' ,
},
{
refreshToken: currentRefreshToken ,
}
);
Token Rotation : Each refresh invalidates the old refresh token and returns a new one. If a revoked token is reused, all tokens for that app are revoked as a security measure.
Token Exchange Grant
Exchange an access token for a delegated token to access another app’s resources. This enables connector flows where one app accesses another app’s API on behalf of a user.
Request Body
Must be urn:ietf:params:oauth:grant-type:token-exchange
The access token or access token JWT from the source app
Resource key of the target resource to access
Space-separated scopes being requested for the target resource
Your OAuth application’s client ID
Your OAuth application’s client secret
Optional actor information to include in the delegated token
Response
Delegated access token (JWT) for the target resource
Token lifetime in seconds (default: 600)
Granted scopes for the target resource
Audience claim for the target resource
Resource key of the target resource
Communication mode: user_present or background
Example
import { exchangeDelegatedToken } from '@ave-id/sdk' ;
const delegatedToken = await exchangeDelegatedToken (
{
clientId: 'your_client_id' ,
redirectUri: 'https://yourapp.com/callback' ,
},
{
subjectToken: accessTokenJwt ,
requestedResource: 'target-app-resource' ,
requestedScope: 'read write' ,
actor: { service: 'integration-service' },
}
);
Delegation Grant Required : The user must have previously authorized delegation between your app and the target resource. See the Connector Flow guide for more information.
Error Responses
All grant types return standard OAuth 2.0 error responses:
Error code:
invalid_client - Client authentication failed
invalid_grant - Authorization code or refresh token is invalid/expired
invalid_request - Missing or invalid parameters
invalid_scope - Requested scope is not allowed
invalid_target - Requested resource not found
access_denied - No delegation grant exists
Human-readable error description
Example Error
{
"error" : "invalid_grant" ,
"error_description" : "Authorization code expired"
}