【问题标题】:JWT Authentication with Airflow API使用 Airflow API 进行 JWT 身份验证
【发布时间】:2021-08-19 05:50:05
【问题描述】:

我正在尝试实施基于令牌的身份验证作为触发气流 dag 的一部分。有没有办法我们可以添加 JWT 身份验证来生成 access_token 来触发 API?非常感谢任何帮助!

【问题讨论】:

    标签: api authentication jwt airflow


    【解决方案1】:

    我们的身份验证服务返回一个JSON 响应,如下所示:

    {
        "clientToken": "322e8df6-0597-479e-984d-db6d8705ee66"
    }
    

    这是我在气流 2.1 中使用SimpleHttpOperatorXCOM 变量传递机制来克服这个问题的示例代码:

        get_token = SimpleHttpOperator(
            task_id='get_token',
            method='POST',
            http_conn_id='http_service',
            data=json.dumps( {
                "username": "user_name",
                "password": "n46r4A83"
            }),
            endpoint='/authenticate',
            dag=dag,
            headers = {
            'Content-Type': 'application/json',
            'Cache-Control': 'no-cache',
            },
            response_filter=lambda response: response.json()['clientToken'],
        )
    
        
        get_cities = SimpleHttpOperator(
            task_id='get_cities',
            method='GET',
            http_conn_id='http_service',
            endpoint='/cities?dsCode=120',
            dag=dag,
            headers = {
            'X-CLIENT-TOKEN':'{{ ti.xcom_pull(task_ids="get_token") }}'
            }
            # response_filter=lambda response: response.json()['clientToken'],
        )
    
        get_token >> get_cities
    
    

    【讨论】:

      猜你喜欢
      • 2016-05-18
      • 1970-01-01
      • 2016-04-07
      • 2023-03-27
      • 2021-05-29
      • 2017-07-30
      • 2018-06-30
      • 2018-08-03
      • 2021-02-06
      相关资源
      最近更新 更多