![]() ![]() Return _by(device_key=device_key).first() ![]() Return find_by_device_key(cls, device_key): 'user_id': find_by_name(cls, device_name): vice_key = device_key or uuid.uuid4().hex User = db.relationship('UserModel', back_populates="devices")ĭef _init_(self, device_name, user_id, device_key=None): User_id = db.Column(db.Integer, db.ForeignKey('users.id')) Id = db.Column(db.Integer, primary_key=True) I'll also add a few helper methods to the model so it's easier to interact with from our views later on: from db import db user_id, a one-to-many relationship with users, so we know which devices are owned by which users.They can use this to make requests to some of our API endpoints. device_key, the API key that each device will be given.device_name, a descriptive string for each device.id, which is a unique auto-incrementing identifier, for internal use.In the application, I'll refer to "non-human users" as "devices", so it's simpler. Let's start by creating a model, which I'll call DeviceModel, to store said data. Since we're using SQLAlchemy, the first step should be to decide how we want to store data about our "non-human users" and the API keys that we've given them. How to generate and store API keys in your database Creating the DeviceModel If you want to look at all the code needed to add API authentication in one place, you can do so here. If you'd like to use Flask-RESTX and Flask-JWT-Extended instead, the changes required are minimal! In this post, let me show you how to add API key authentication to your Flask app! We will use the same libraries as we do in our REST APIs with Flask and Python course: Whenever they make a request to your API they'll send the API key, and that authenticates and identifies them. An API key is similar to a password, and is usually given to non-human users of your API.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |