> ## Documentation Index
> Fetch the complete documentation index at: https://finance.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# OAuth2 Token

> Exchanges authorization code for access and refresh tokens, or refreshes tokens. This endpoint serves two purposes: 1) Authorization Code Grant - exchanges authorization code for tokens (supports both traditional flow with client_secret and PKCE flow with code_verifier), 2) Refresh Token Grant - exchanges refresh token for new access token.



## OpenAPI

````yaml https://spec.speakeasy.com/aries/aries/aries-trading-platform-api-with-code-samples post /v1/oauth2/token
openapi: 3.0.3
info:
  title: Aries Trading Platform API
  description: >-
    Complete API documentation for the Aries trading platform including user
    management, authentication, account management, order management, market
    data, and analytics
  contact:
    name: Aries Financial
    email: dev@aries.com
  version: 1.0.0
servers:
  - url: https://api.tradearies.dev
    description: Production server
security: []
tags:
  - name: Accounts
    description: Account management endpoints for positions, orders, and balances
  - name: Analytics
    description: >-
      Analytics endpoints for market data analysis including top gainers,
      losers, volume leaders, sector analysis, analyst ratings, market breadth,
      and net inflow
  - name: Authentication
    description: User authentication endpoints
  - name: Calendars
    description: Calendar endpoints for economic events and historical data
  - name: Market Data
    description: >-
      Market data endpoints for symbol search, real-time data access, and equity
      details
  - name: Orders
    description: >-
      Order management endpoints for placing, updating, canceling, and
      previewing orders
  - name: Users
    description: User management endpoints
  - name: Health
    description: Service health endpoints
paths:
  /v1/oauth2/token:
    post:
      tags:
        - OAuth2
      summary: OAuth2 Token
      description: >-
        Exchanges authorization code for access and refresh tokens, or refreshes
        tokens. This endpoint serves two purposes: 1) Authorization Code Grant -
        exchanges authorization code for tokens (supports both traditional flow
        with client_secret and PKCE flow with code_verifier), 2) Refresh Token
        Grant - exchanges refresh token for new access token.
      operationId: oauth2_Token
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties:
                grant_type:
                  type: string
                  enum:
                    - authorization_code
                    - refresh_token
                  description: OAuth2 grant type
                code:
                  type: string
                  description: >-
                    Authorization code from /v1/oauth2/authorize/confirm
                    (required for authorization_code grant)
                client_id:
                  type: string
                  description: OAuth2 client identifier
                client_secret:
                  type: string
                  description: >-
                    Client secret (required for confidential clients and
                    refresh_token grant)
                code_verifier:
                  type: string
                  description: >-
                    PKCE code verifier (required for PKCE flow, replaces
                    client_secret)
                redirect_uri:
                  type: string
                  format: uri
                  description: >-
                    Redirect URI (required for authorization_code grant, must
                    match authorization request)
                refresh_token:
                  type: string
                  description: Refresh token (required for refresh_token grant)
              required:
                - grant_type
                - client_id
            examples:
              authorization_code_traditional:
                summary: Authorization Code Grant (Traditional)
                value:
                  grant_type: authorization_code
                  code: auth_code_abc123xyz789def456
                  client_id: client_abc123xyz
                  client_secret: secret_xyz789abc
                  redirect_uri: https://yourapp.com/callback
              authorization_code_pkce:
                summary: Authorization Code Grant (PKCE)
                value:
                  grant_type: authorization_code
                  code: auth_code_abc123xyz789def456
                  client_id: client_abc123xyz
                  code_verifier: dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
                  redirect_uri: https://yourapp.com/callback
              refresh_token:
                summary: Refresh Token Grant
                value:
                  grant_type: refresh_token
                  refresh_token: refresh_token_xyz789abc456def
                  client_id: client_abc123xyz
                  client_secret: secret_xyz789abc
        required: true
      responses:
        '200':
          description: Tokens generated successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  access_token:
                    type: string
                    description: Bearer token for API authentication
                  refresh_token:
                    type: string
                    description: Token to refresh the access token
                  token_type:
                    type: string
                    enum:
                      - Bearer
                    description: Token type (always Bearer)
                  expires_in:
                    type: integer
                    description: >-
                      Access token expiration time in seconds (typically 3600 =
                      1 hour)
                  refresh_token_expires_in:
                    type: integer
                    description: >-
                      Refresh token expiration time in seconds (typically
                      2592000 = 30 days)
                  scope:
                    type: string
                    description: Space-separated list of granted scopes
                required:
                  - access_token
                  - token_type
                  - expires_in
              example:
                access_token: >-
                  eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
                refresh_token: >-
                  eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.cThIIoDvwdueQB468K5xDc5633seEFoqwxjF_xSJyQQ
                token_type: Bearer
                expires_in: 3600
                refresh_token_expires_in: 2592000
                scope: read write
        '400':
          description: Bad request - invalid grant, malformed request, or validation error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                invalid_grant:
                  summary: Invalid authorization code
                  value:
                    error: Invalid grant
                    codes:
                      - code: INVALID_GRANT
                        field: code
                        description: >-
                          Authorization code is invalid, expired, or already
                          used
                redirect_uri_mismatch:
                  summary: Redirect URI mismatch
                  value:
                    error: Redirect URI mismatch
                    codes:
                      - code: REDIRECT_URI_MISMATCH
                        field: redirect_uri
                        description: >-
                          The redirect_uri must match the one used in the
                          authorization request
        '401':
          description: >-
            Unauthorized - invalid client credentials or expired/invalid refresh
            token
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
              examples:
                invalid_client:
                  summary: Invalid client credentials
                  value:
                    error: Invalid client
                    codes:
                      - code: INVALID_CLIENT
                        description: >-
                          Client authentication failed - invalid client_id or
                          client_secret
                invalid_refresh_token:
                  summary: Invalid refresh token
                  value:
                    error: Invalid refresh token
                    codes:
                      - code: INVALID_REFRESH_TOKEN
                        field: refresh_token
                        description: >-
                          The refresh token is invalid, expired, or has been
                          revoked
        '500':
          description: Internal server error
      x-codeSamples:
        - lang: java
          label: Java (SDK)
          source: |-
            package hello.world;

            import java.lang.Exception;
            import org.openapis.openapi.AriesJava;
            import org.openapis.openapi.models.errors.ErrorResponse;
            import org.openapis.openapi.models.operations.*;

            public class Application {

                public static void main(String[] args) throws ErrorResponse, Exception {

                    AriesJava sdk = AriesJava.builder()
                            .bearerAuth(System.getenv().getOrDefault("BEARER_AUTH", ""))
                        .build();

                    Oauth2TokenRequest req = Oauth2TokenRequest.builder()
                            .grantType(GrantType.AUTHORIZATION_CODE)
                            .clientId("client_abc123xyz")
                            .code("auth_code_abc123xyz789def456")
                            .clientSecret("secret_xyz789abc")
                            .redirectUri("https://yourapp.com/callback")
                            .build();

                    Oauth2TokenResponse res = sdk.oauth2().token()
                            .request(req)
                            .call();

                    if (res.object().isPresent()) {
                        // handle response
                    }
                }
            }
components:
  schemas:
    ErrorResponse:
      type: object
      properties:
        error:
          type: string
          description: Error message
        codes:
          type: array
          items:
            type: object
            properties:
              code:
                type: string
                description: Error code
              description:
                type: string
                description: Error description

````