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

# Get a workspace OAuth access token for an app

> Exchange a workspace machine client's ID and secret for an e6data OAuth 2.0 access token. See the form fields, response, and error codes.

API referenceApplies to: Workspaces with machine client access

`POST /oauth2/token` issues an access token for an app using the OAuth 2.0 client credentials grant. Send the request to the HTTPS URL of the workspace that issued the client ID. For setup and a first API call, see [List workspace clusters with OAuth](https://docs.e6data.com/api-reference/list-clusters-with-oauth.md).

## Choose the token request for your workspace

Ask your workspace administrator how cluster API access is checked for this app.

| How the workspace checks access | How to request a token to read clusters |
| - | - |
| Scopes assigned to the app | Request `query_engine.clusters:read`. The administrator must have assigned it to the app. |
| Permissions granted to the app on clusters | Omit the `scope` field for this management call. Ask the administrator to grant the app read access to the intended cluster. Requesting `query_engine.clusters:read` can return `invalid_scope` even when the app can read that cluster. |

For an app that runs SQL through the query engine, the token still needs `query_engine.query:execute`. Confirm SQL access and access to the target cluster before running a query.

## Request

Send the client ID and secret with **HTTP Basic** authentication. Use an `application/x-www-form-urlencoded` body.

| Form field | What to send |
| - | - |
| `grant_type` | Required. Use `client_credentials`. |
| `scope` | Optional. A space-separated list limits the scopes requested for this token. If omitted, the workspace issues the scopes available to this client. |
| `resource` or `audience` | Optional. Use one only if your workspace provides a specific target for this integration. Omit both to use the default workspace API target. |

This Bash example requests a token for an app with an assigned cluster-read scope. If your administrator confirms that the workspace checks permissions on clusters, set `SCOPE_ARGS=()` instead. Curl prompts for the client secret. Keep Bash tracing off when handling tokens. The command displays the token type, lifetime, and scope without printing the token itself. It requires curl 7.76 or later and jq.

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
set +x
WORKSPACE_URL='https://your-workspace.example'
CLIENT_ID='your-client-id'
SCOPE_ARGS=(--data-urlencode 'scope=query_engine.clusters:read')

unset ACCESS_TOKEN
TOKEN_REPLY=$(curl --proto '=https' --silent --show-error --fail-with-body \
  --user "$CLIENT_ID" \
  --data 'grant_type=client_credentials' \
  "${SCOPE_ARGS[@]}" \
  "$WORKSPACE_URL/oauth2/token") &&
  ACCESS_TOKEN=$(jq -er '.access_token | strings | select(length > 0)' <<< "$TOKEN_REPLY") &&
  jq '{token_type, expires_in, scope}' <<< "$TOKEN_REPLY"
```

## Response

HTTP `200` returns JSON. This example shows a client with the cluster-read scope. The access token is redacted and the lifetime is illustrative. Use the `expires_in` value returned by your workspace, in seconds.

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "access_token": "<redacted>",
  "token_type": "Bearer",
  "expires_in": 900,
  "scope": "query_engine.clusters:read"
}
```

When the workspace checks cluster permissions, the returned scope can be an empty string. Confirm access by [reading a cluster the app should see](https://docs.e6data.com/api-reference/workspace/get-api-v1-queryservices-name.md). Send the token in the `Authorization: Bearer` header. To continue from the Bash example, use [the cluster list request](https://docs.e6data.com/api-reference/list-clusters-with-oauth.md#2-list-the-clusters) in the same terminal. For a recurring job, [request a replacement before expiry](https://docs.e6data.com/api-reference/list-clusters-with-oauth.md#keep-a-scheduled-job-signed-in). This grant does not return a refresh token.

## Errors

| HTTP status and error | What to check |
| - | - |
| `400 invalid_scope` | Check the scope spelling and [choose the token request for your workspace](#choose-the-token-request-for-your-workspace). |
| `400 invalid_target` | Check the target with your workspace admin. Send at most one of `resource` and `audience`. |
| `400 unauthorized_client` | Ask the client owner to confirm this machine client can use the client credentials grant. |
| `400 unsupported_grant_type` | Check the form encoding and the `grant_type` value. |
| `401 invalid_client` | Check the workspace URL, client ID, current secret, and whether the client is enabled. |
| `500 server_error` | If the error persists, contact support with the time and workspace URL. Keep credentials out of the report. |
| `503 temporarily_unavailable` | Wait for the interval in the `Retry-After` header before trying again. |

An error response contains an `error` field, for example `{"error":"invalid_scope"}`. If curl reported an HTTP error, use `jq -r '.error // empty' <<< "$TOKEN_REPLY"` to see its name. Keep the full token reply out of logs. When you finish making API calls in this shell, run `unset ACCESS_TOKEN TOKEN_REPLY`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.