โค้ด: เลือกทั้งหมด
response = requests.post(link_auth, headers=headers, data=data)
โค้ด: เลือกทั้งหมด
@app.route('/gentokenuser')
@auth.login_required
def gentokenuser():
cur = con.cursor()
sql = """SELECT * FROM "user" WHERE username = %s;"""
try:
cur.execute(sql, (auth.username(),))
results = cur.fetchone()
if not results:
return jsonify({'error': 'User not found'}), 404
link_auth = 'http://{}:{}/auth'.format(ip, port)
headers = {
'Content-Type': 'application/json',
}
data = {
'username': results[3],
'password': results[4]
}
response = requests.post(link_auth, headers=headers, json=data)
response.raise_for_status()
token_user = response.json().get('access_token')
if not token_user:
return jsonify({'error': 'Token not received'}), 400
sql_update = """UPDATE public."user" SET token = %s WHERE id = %s;"""
cur.execute(sql_update, (token_user, results[0]))
con.commit()
except requests.exceptions.RequestException as e:
return jsonify({'error': 'HTTP Request failed', 'details': str(e)}), 500
except p.DatabaseError as e:
return jsonify({'error': 'Database error', 'details': str(e)}), 500
except Exception as e:
return jsonify({'error': 'An unexpected error occurred', 'details': str(e)}), 500
return jsonify({'access_token': token_user})
หากแต่เมื่อฉันทำการทดสอบบน python3 เจอบัค
โดยสิ่งแวดล้อมทุกอย่างเหมือนกันหมด{
"details": "404 Client Error: NOT FOUND for url: http://127.0.0.1:5005/auth",
"error": "HTTP Request failed"
}
auth = HTTPBasicAuth()
ต่างกันในส่วนของ Python2
jwt = JWT(app, authenticate, identity)
import จาก from flask_jwt import JWT
ในส่วนของ Python3
jwt = JWTManager(app)
import จาก from flask_jwt_extended import JWTManager
ขอทราบปัญหาและแนวทางในการแก้ไขครับ