@@ -178,14 +178,6 @@ For details on enabling/configuring CORS, see
178178To be able to meet the requirements of many organizations, Airflow supports many authentication methods,
179179and it is even possible to add your own method.
180180
181- If you want to check which auth backend is currently set, you can use
182- ` airflow config get-value api auth_backends ` command as in the example below.
183-
184- ``` bash
185- $ airflow config get-value api auth_backends
186- airflow.providers.fab.auth_manager.api.auth.backend.basic_auth
187- ```
188-
189181The default is to deny all requests.
190182
191183For details on configuring the authentication, see
@@ -279,24 +271,60 @@ import airflow_client.client
279271
280272## Getting Started
281273
274+ Before attempting the following examples ensure you have an account with API access.
275+ As an example you can create an account for usage with the API as follows using the Airflow CLI.
276+
277+ ``` bash
278+ airflow users create -u admin-api -e
[email protected] -f admin-api -l admin-api -p
$PASSWORD -r Admin
279+ ```
280+
282281Please follow the [ installation procedure] ( #installation--usage ) and then run the following:
283282
284283``` python
285284import airflow_client.client
285+ import requests
286286from airflow_client.client.rest import ApiException
287287from pprint import pprint
288+ from pydantic import BaseModel
289+
290+
291+ # What we expect back from auth/token
292+ class AirflowAccessTokenResponse (BaseModel ):
293+ access_token: str
294+
295+ # An optional helper function to retrieve an access token
296+ def get_airflow_client_access_token (
297+ host : str ,
298+ username : str ,
299+ password : str ,
300+ ) -> str :
301+ url = f " { host} /auth/token "
302+ payload = {
303+ " username" : username,
304+ " password" : password,
305+ }
306+ headers = {" Content-Type" : " application/json" }
307+ response = requests.post(url, json = payload, headers = headers)
308+ if response.status_code != 201 :
309+ raise RuntimeError (f " Failed to get access token: { response.status_code} { response.text} " )
310+ response_success = AirflowAccessTokenResponse(** response.json())
311+ return response_success.access_token
288312
289313# Defining the host is optional and defaults to http://localhost
290314# See configuration.py for a list of all supported configuration parameters.
291- configuration = airflow_client.client.Configuration(host = " http://localhost" )
315+ host = " http://localhost"
316+ configuration = airflow_client.client.Configuration(host = host)
292317
293318# The client must configure the authentication and authorization parameters
294319# in accordance with the API server security policy.
295320# Examples for each auth method are provided below, use the example that
296321# satisfies your auth use case.
297322
298- configuration.access_token = os.environ[" ACCESS_TOKEN" ]
299-
323+ configuration.access_token = get_airflow_client_access_token(
324+ host = host,
325+ username = " admin-api" ,
326+ password = os.environ[" PASSWORD" ],
327+ )
300328
301329# Enter a context with an instance of the API client
302330with airflow_client.client.ApiClient(configuration) as api_client:
0 commit comments