# Login

`POST /api/v1/users/token`

With your [client id and secret](#operation/getOAuthClient), you can retrieve an access and refresh tokens.

Authentication: no security requirement declared for this operation.

Parameters:

- `x-peertube-otp` (header, string, optional) - If the user enabled two factor authentication, you need to provide the OTP code in this header
  Type: string

Request body: application/x-www-form-urlencoded

Type: OAuthClient & object

Discriminator:

```json
{
  "propertyName": "grant_type",
  "mapping": {
    "password": "#/components/schemas/OAuthToken-password",
    "refresh_token": "#/components/schemas/OAuthToken-refresh_token"
  }
}
```

oneOf:
  Reference: [OAuthToken-password](https://sourcey-peertube.pages.dev/api-rest-reference/models/oauthtoken-password.md); Type: OAuthClient & object

  Reference: [OAuthToken-refresh_token](https://sourcey-peertube.pages.dev/api-rest-reference/models/oauthtoken-refresh-token.md); Type: OAuthClient & object

Responses:

- 200: successful operation

Content type: application/json

Type: object

- `token_type` (optional)
  Type: string
  
  Example:
  
  ```json
  "Bearer"
  ```

- `access_token` (optional)
  Type: string
  
  valid for 1 day
  
  Example:
  
  ```json
  "90286a0bdf0f7315d9d3fe8dabf9e1d2be9c97d0"
  ```

- `refresh_token` (optional)
  Type: string
  
  valid for 2 weeks
  
  Example:
  
  ```json
  "2e0d675df9fc96d2e4ec8a3ebbbf45eca9137bb7"
  ```

- `expires_in` (optional)
  Type: integer
  
  minimum: 0
  
  Example:
  
  ```json
  14399
  ```

- `refresh_token_expires_in` (optional)
  Type: integer
  
  minimum: 0
  
  Example:
  
  ```json
  1209600
  ```

- 400: Disambiguate via `code`:
- `invalid_client` for an unmatched `client_id`
- `invalid_grant` for unmatched credentials


- 401: Disambiguate via `code`:
- default value for a regular authentication failure
- `invalid_token` for an expired token
- `missing_two_factor` if two factor header is missing


## Examples

### Shell

```Shell
## DEPENDENCIES: jq
API="https://peertube2.cpy.re/api/v1"
USERNAME="<your_username>"
PASSWORD="<your_password>"

## AUTH
client_id=$(curl -s "$API/oauth-clients/local" | jq -r ".client_id")
client_secret=$(curl -s "$API/oauth-clients/local" | jq -r ".client_secret")
curl -s "$API/users/token" \
  --data client_id="$client_id" \
  --data client_secret="$client_secret" \
  --data grant_type=password \
  --data username="$USERNAME" \
  --data-urlencode password="$PASSWORD" \
  | jq -r ".access_token"
```
