'Apache Airflow - rest API Authentication

I am trying to trigger my dag with AirFlow rest API. However not able to understand how to do authentication.

Following URL works fine from my browser.

http://localhost:8181/api/experimental/dags/demo/dag_runs

However, Following code gives Authentication Error.

import requests
import json
from pprint import pprint


result = requests.get(
  "http://localhost:8181/api/experimental/dags/demo/dag_runs",
  data=json.dumps("{}"),
  auth=("myuser", "mypassword"))
pprint(result.content.decode('utf-8'))

I found this as well, but now sure how to pass auth

https://github.com/apache/airflow/blob/master/airflow/api/client/api_client.py



Solution 1:[1]

By default the REST API rejects all requests. Make sure to set up the API auth backend as described in the docs:

[api]
auth_backend = airflow.api.auth.backend.basic_auth

Solution 2:[2]

1.- You should verify that the authentication is working on your UI:

http://localhost:8181/api/experimental/dags/demo/dag_runs

This link should require authentication.

2.- The request code snippet you could try is the following:

curl --user USER_NAME:USER_PASSWORD -X POST \
http://localhost:8181/api/experimental/dags/demo/dag_runs \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d "{}"

Similar code in node: https://godatadriven.com/blog/using-the-airflow-experimental-rest-api-to-trigger-a-dag/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 0x5453
Solution 2 jameslimousin