Python Forum
Python3 - How to deny access to users without api key using localhost and postman
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 - How to deny access to users without api key using localhost and postman
#1
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 ?
Larz60+ write Feb-20-2023, 03:22 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Restful API Python Flask Postman starter_student 1 1,802 Sep-22-2019, 08:16 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020