DS Server

POST /oauth/token

Endpoint

POST https://example.com/oauth/token

Description

The token endpoint is used to programmatically request access tokens. It supports the "authorization_code", "client_credentials" and "refresh_token" grant types. The "client_credentials" grant type works only for a security profile that allows it in the admin portal.

An access token is valid for 3600 seconds. The token is also issued for a security profile whose expiry date has passed, but the endpoints then reject it with 401 (Unauthorized).

When DS Server can't authorize the request itself, for example because it doesn't know the client ID, it forwards the request to the registered authentication servers (see POST /authenticationserver/token) and returns their answer.

Examples

This sample requests an access token with the "client_credentials" grant type. The client ID and client secret are sent in a Basic authorization header.

# Request:
curl --request POST 'https://trial.dsserver.io/oauth/token' \
    --user 'yourclientid:yourclientsecret' \
    --data-urlencode 'grant_type=client_credentials'

# Response:
{
    "access_token": "nmZ80Zn2R0P2AbS149E31IUHd5aZgB9T",
    "token_type": "Bearer",
    "expires_in": "3600"
}

This sample exchanges an authorization code returned by GET /oauth/authorize for an access token. The client ID and client secret are sent as form fields, and the redirect URI must be the one the code was requested with. An authorization code can be exchanged only once. The response also contains a refresh token.

# Request:
curl --request POST 'https://trial.dsserver.io/oauth/token' \
    --data-urlencode 'grant_type=authorization_code' \
    --data-urlencode 'code=g0ZGZmNjVmOWIjNTk2NTk4ZTYyZGI3' \
    --data-urlencode 'client_id=yourclientid' \
    --data-urlencode 'client_secret=yourclientsecret' \
    --data-urlencode 'redirect_uri=https://localhost:5001/callback'

# Response:
{
    "access_token": "nmZ80Zn2R0P2AbS149E31IUHd5aZgB9T",
    "token_type": "Bearer",
    "expires_in": "3600",
    "refresh_token": "Ghk7w2LmNq0ZpX4rT8sV1uY6aB3cD9eF"
}

This sample requests a new access token with the refresh token of a previous response. The response contains a new access token and a new refresh token; the refresh token that was sent can't be used again.

# Request:
curl --request POST 'https://trial.dsserver.io/oauth/token' \
    --data-urlencode 'grant_type=refresh_token' \
    --data-urlencode 'refresh_token=Ghk7w2LmNq0ZpX4rT8sV1uY6aB3cD9eF' \
    --data-urlencode 'client_id=yourclientid' \
    --data-urlencode 'client_secret=yourclientsecret'

Authorization

The "client_credentials" grant type requires the Basic authorization method. For the "authorization_code" grant type, the client ID and client secret are sent as the form fields client_id and client_secret instead; a Basic authorization header is not used for this grant type.

Basic

Header fieldDescription
AuthorizationThe client ID and the client secret of the security profile are combined into a string separated by a colon, e.g.: clientid:clientsecret. The resulting string is encoded using the RFC2045-MIME variant of Base64, except not limited to 76 char/line. The authorization method and a space i.e. "Basic " is then put before the encoded string. For example: Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l

Request Payload

TypeValue
TokenDataThe parameters of the TokenData object must be sent using the "application/x-www-form-urlencoded" format with a character encoding of UTF-8 in the HTTP request body payload. A request without form data, for example with a JSON body, is rejected with 405 (Method Not Allowed).

TokenData

NameTypeValueOptional
grant_typeStringThe grant type of the token request. Supported values are "authorization_code", "client_credentials" and "refresh_token".no
codeStringThe authorization code returned by GET /oauth/authorize. Required for the "authorization_code" grant type. A code can be exchanged only once.yes
redirect_uriStringThe redirect URI the authorization code was requested with. It must match one of the authorized redirect URIs of the security profile; upper and lower case are ignored. Required for the "authorization_code" grant type.yes
client_idStringThe client ID of the security profile. Required for the "authorization_code" grant type; for "client_credentials", it is sent in the Basic authorization header.yes
client_secretStringThe client secret of the security profile. Required for the "authorization_code" grant type; for "client_credentials", it is sent in the Basic authorization header.yes
refresh_tokenStringThe refresh token of a previous response. Required for the "refresh_token" grant type. A refresh token can be used only once.yes
scopeStringThe scope of the request.yes

Success Response

StatusDescription
200On success, the HTTP status code in the response header is 200 (OK). It returns a JSON object (application/json) with access_token, token_type ("Bearer") and expires_in (the lifetime in seconds as a string, "3600"). The "authorization_code" and "refresh_token" grant types also return a new refresh_token.

Error Response

The 400 errors return a JSON object (application/json) with the fields error and error_description.

StatusDescription
400invalid_request "Missing authorization header." is returned, if the "client_credentials" grant type is requested without a Basic authorization header. A Basic authorization header that can't be decoded or has no colon returns invalid_request with a description of the problem, and another authorization type returns "Unexpected authorization type. (Bearer)".
400invalid_request "Missing 'client_id' parameter.", "Missing 'code' parameter." or "Missing 'redirect_uri' parameter." is returned, if a form field required by the "authorization_code" grant type is missing, and "Missing 'refresh_token' parameter.", if the "refresh_token" grant type has no refresh token.
400invalid_client "Invalid client ID." or "Invalid client secret." is returned, if the client credentials don't match a security profile.
400unsupported_grant_type "This security profile does not allow the specified grant type. (client_credentials)" is returned, if the grant type is not supported or not allowed for the security profile. The grant type that was sent is given in parentheses.
400invalid_grant "Invalid authorization code." is returned, if the authorization code is not valid, was already used or was requested with another redirect URI.
400invalid_grant "Invalid refresh token." is returned, if the refresh token is not valid or was already used.
405A 405 (Method Not Allowed) with a problem details object (application/problem+json) is returned, if the body is not form data.