hello i am trying to do the same stuff getting the same error here is my code its just practice code so info is secret
from flask import Flask, request, jsonify, make_response
from flask_sqlalchemy import SQLAlchemy
import uuid
from werkzeug.security import generate_password_hash, check_password_hash
import jwt
import datetime
from functools import wraps
app = Flask(name)
app.config['SECRET_KEY'] = 'thereisnosecret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://lstapp:legend11@lstapp.mysql.pythonanywhere-services.com/lstapp$testdata'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
public_id = db.Column(db.String(50), unique=True)
timestmp = db.Column(db.Integer)
password = db.Column(db.String(80))
admin = db.Column(db.Boolean)
Textfield1 = db.Column(db.String(50))
Textfield2 = db.Column(db.String(50))
Numberfield1 = db.Column(db.Integer)
Numberfield2 = db.Column(db.Integer)
def token_required(f):
@wraps(f)
def decorated(args, *kwargs):
token = None
if 'x-access-token' in request.headers:
token = request.headers['x-access-token']
if not token:
return jsonify({'message' : 'Token is missing!'}), 401
try:
data = jwt.decode(token, app.config['SECRET_KEY'])
current_user = User.query.filter_by(public_id=data['public_id']).first()
except:
return jsonify({'message' : 'Token is invalid!'}), 401
return f(current_user, *args, **kwargs)
return decorated
@app.route('/user', methods=['GET'])
@token_required
def get_all_users(current_user):
if not current_user.admin:
return jsonify({'message' : 'Cannot perform that function!'})
users = User.query.all()
output = []
for user in users:
user_data = {}
user_data['public_id'] = user.public_id
user_data['timestmp'] = user.timestmp
user_data['password'] = user.password
user_data['admin'] = user.admin
user_data['TextField1'] = user.Textfield1
user_data['TextField2'] = user.Textfield2
user_data['Numberfield1'] = user.Numberfield1
user_data['Numberfield2'] = user.Numberfield2
output.append(user_data)
return jsonify({'users' : output})
@app.route('/user/<public_id>', methods=['GET'])
@token_required
def get_one_user(current_user, public_id):
if not current_user.admin:
return jsonify({'message' : 'Cannot perform that function!'})
user = User.query.filter_by(public_id=public_id).first()
if not user:
return jsonify({'message' : 'No user found!'})
user_data = {}
user_data['public_id'] = user.public_id
user_data['timestmp'] = user.timestmp
user_data['password'] = user.password
user_data['admin'] = user.admin
user_data['TextField1'] = user.Textfield1
user_data['TextField2'] = user.Textfield2
user_data['Numberfield1'] = user.Numberfield1
user_data['Numberfield2'] = user.Numberfield2
return jsonify({'user' : user_data})
@app.route('/user', methods=['POST'])
@token_required
def create_user(current_user):
if not current_user.admin:
return jsonify({'message' : 'Cannot perform that function!'})
data = request.get_json()
hashed_password = generate_password_hash(data['password'], method='sha256')
new_user = User(public_id=str(uuid.uuid4()), timestmp=datetime.now(), password=hashed_password, admin=False)
db.session.add(new_user)
db.session.commit()
return jsonify({'message' : 'New user created!'})
@app.route('/user/<public_id>', methods=['PUT'])
@token_required
def promote_user(current_user, public_id):
if not current_user.admin:
return jsonify({'message' : 'Cannot perform that function!'})
user = User.query.filter_by(public_id=public_id).first()
if not user:
return jsonify({'message' : 'No user found!'})
user.admin = True
db.session.commit()
return jsonify({'message' : 'The user has been promoted!'})
@app.route('/user/<public_id>', methods=['DELETE'])
@token_required
def delete_user(current_user, public_id):
if not current_user.admin:
return jsonify({'message' : 'Cannot perform that function!'})
user = User.query.filter_by(public_id=public_id).first()
if not user:
return jsonify({'message' : 'No user found!'})
db.session.delete(user)
db.session.commit()
return jsonify({'message' : 'The user has been deleted!'})
@app.route('/login')
def login():
auth = request.authorization
if not auth or not auth.username or not auth.password:
return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
user = User.query.filter_by(name=auth.username).first()
if not user:
return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
if check_password_hash(user.password, auth.password):
token = jwt.encode({'public_id' : user.public_id, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}, app.config['SECRET_KEY'])
return jsonify({'token' : token.decode('UTF-8')})
return make_response('Could not verify', 401, {'WWW-Authenticate' : 'Basic realm="Login required!"'})
if name == 'main':
app.run(debug=True)