@@ -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,62 @@ 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 admin-api@example.com -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+
296+ # An optional helper function to retrieve an access token
297+ def get_airflow_client_access_token (
298+ host : str ,
299+ username : str ,
300+ password : str ,
301+ ) -> str :
302+ url = f " { host} /auth/token "
303+ payload = {
304+ " username" : username,
305+ " password" : password,
306+ }
307+ headers = {" Content-Type" : " application/json" }
308+ response = requests.post(url, json = payload, headers = headers)
309+ if response.status_code != 201 :
310+ raise RuntimeError (f " Failed to get access token: { response.status_code} { response.text} " )
311+ response_success = AirflowAccessTokenResponse(** response.json())
312+ return response_success.access_token
313+
288314
289315# Defining the host is optional and defaults to http://localhost
290316# See configuration.py for a list of all supported configuration parameters.
291- configuration = airflow_client.client.Configuration(host = " http://localhost" )
317+ host = " http://localhost"
318+ configuration = airflow_client.client.Configuration(host = host)
292319
293320# The client must configure the authentication and authorization parameters
294321# in accordance with the API server security policy.
295322# Examples for each auth method are provided below, use the example that
296323# satisfies your auth use case.
297324
298- configuration.access_token = os.environ[" ACCESS_TOKEN" ]
299-
325+ configuration.access_token = get_airflow_client_access_token(
326+ host = host,
327+ username = " admin-api" ,
328+ password = os.environ[" PASSWORD" ],
329+ )
300330
301331# Enter a context with an instance of the API client
302332with airflow_client.client.ApiClient(configuration) as api_client:
0 commit comments