Python Forum
Python3 - How to deny access to users without api key using localhost and postman - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Python3 - How to deny access to users without api key using localhost and postman (/thread-39450.html)



Python3 - How to deny access to users without api key using localhost and postman - Alan2023 - Feb-20-2023

Hi everybody,
I am using python and Flask, and I need to deny access for not authorized users, users without an api key will not be allowed to access the routers. I created some code but it does not work. Any information will be welcome.

app = Flask(__name__)


with open("config.yaml", "r") as c:
    conf = yaml.safe_load(c)
    servidor = conf["servidor"]
    puerto = conf["puerto"]
    consumidor = conf["consumidor_almacen"]
    api_key = conf["consumidor_almacen_key"]
    c.close()


headers = {"api_key": api_key, "consumidor": consumidor}


@app.route( "/", methods=['GET'] )
def productos():
    print(headers)
    
    #  HERE IS MY DOUBT, I DO NOT KNOW HOT TO USE IT TO DENY ACCESS OR PERMIT ACCESS
    url = "http://localhost:" + str(puerto)
    r = requests.get(url, headers=headers)
    
    query = "SELECT * FROM productos"
    return conexion.conect(query, False)
In my POSTMAN, All my routers works, I can make a crud but if I use my api key code using a header, it does not work, it stays reloading and does not show me records from my database sqlite3

In my postman, the key = api_key
Value = {{api_key}} , here I put my global variable and inside of that is my api key code
Add to = Headers

Any solution please ?