Search code examples
pythonauthenticationflaskapache-nifi

Start a NIFI processor using Python SDK of Apache Nifi


I have a python code which is trying to authenticate to my Nifi server using username and password to start a processor inside it. Below is my code:

from flask import Flask
import nipyapi
import requests
 
app = Flask(__name__)

@app.route('/')
def home():
    nifi_url = "https://mynifiinstance:port/nifi-api/access"
    processor_id = <processorID>
    nifi_general = "https://mynifiinstance/nifi-api"
    start_processor_url = f"{nifi_general}/processors/{processor_id}/run-status"
 
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    username = "username"
    password = "password"
    data = { "username": username,
            "password": password
        }
 
 
    response = requests.post(url=nifi_url, data=data,verify=False, headers=headers)
 
    if response.status_code == 200:
        # if successfully authenticated, then start the processor
        start_processor = requests.put(url=start_processor_url, headers=headers)
        print(start_processor.status_code)
        if start_processor.status_code == 200:
            return("Processor started successfully.")
        else:
            return(f"Starting processor failed. Status code: {start_processor.status_code}")
    else:
        return(f"Failed to start processor. Status code: {response.status_code}")
   
if __name__ == '__main__':
    app.run()

However, I am unable to authenticate and am getting 201 as a response. Note: that I have not made any changes in the nifi.properties file. Does it have to be changed? Looking fwd to your responses.


Solution

  • https://nifi.apache.org/docs/nifi-docs/rest-api/index.html

    according to doc you should be using POST /access/token to get access token

    The token returned is formatted as a JSON Web Token (JWT). The token is base64 encoded and comprised of three parts. The header, the body, and the signature. The expiration of the token is a contained within the body. It is stored in the browser as a cookie, but also returned inthe response body to be stored/used by third party client scripts.