I try to send access_tokken from VUE template to Python api but it always provide me error 422. What could be problem? To send jwt token in vue I use this:
const response = await axios.get('http://127.0.0.1:5000/user_info', {
headers: {
Authorization: `JWT ${Cookies.get('access_token')}`
}
});
Pyhton api where is send JWT token
@app.route('/user_info', methods=['GET'])
@jwt_required()
def user_info():
current_user = get_jwt_identity()
app.logger.info(f"Current user: {current_user}")
db = connection_pool.get_connection()
cursor = db.cursor(dictionary=True)
try:
cursor.execute("SELECT name, surname, email FROM client_data WHERE id = %s", (current_user['id'],))
user_info = cursor.fetchone()
app.logger.info(f"User info: {user_info}") # Log the user info for debugging
return jsonify(user_info), 200
except mysql.connector.Error as err:
app.logger.error(f"Error fetching user info: {err}") # Log the error for debugging
return jsonify({"error": str(err)}), 500
finally:
cursor.close()
db.close()
Part of VUE code which send JWT token
let token = Cookies.get('access_token');
if (token) {
token = token.toString();
console.log('Token:', token);
try {
const response = await
axios.get('http://127.0.0.1:5000/user_info', {
headers: {
Authorization: `Bearer ${token}`
}
});
this.user = response.data;
} catch (error) {
console.error('Error fetching user info:', error);
}
}
I try to send access_tokken from VUE template to Python api but it always provide me error 422. What could be problem? To send jwt token in vue I use this:
const response = await axios.get('http://127.0.0.1:5000/user_info', {
headers: {
Authorization: `JWT ${Cookies.get('access_token')}`
}
});
Pyhton api where is send JWT token
@app.route('/user_info', methods=['GET'])
@jwt_required()
def user_info():
current_user = get_jwt_identity()
app.logger.info(f"Current user: {current_user}")
db = connection_pool.get_connection()
cursor = db.cursor(dictionary=True)
try:
cursor.execute("SELECT name, surname, email FROM client_data WHERE id = %s", (current_user['id'],))
user_info = cursor.fetchone()
app.logger.info(f"User info: {user_info}") # Log the user info for debugging
return jsonify(user_info), 200
except mysql.connector.Error as err:
app.logger.error(f"Error fetching user info: {err}") # Log the error for debugging
return jsonify({"error": str(err)}), 500
finally:
cursor.close()
db.close()
Part of VUE code which send JWT token
let token = Cookies.get('access_token');
if (token) {
token = token.toString();
console.log('Token:', token);
try {
const response = await
axios.get('http://127.0.0.1:5000/user_info', {
headers: {
Authorization: `Bearer ${token}`
}
});
this.user = response.data;
} catch (error) {
console.error('Error fetching user info:', error);
}
}
Share
Improve this question
edited Feb 5 at 19:31
santeri pohjaranta
asked Feb 5 at 14:43
santeri pohjarantasanteri pohjaranta
296 bronze badges
4
- Try to debug it with Postman first to narrow down if it's a frontend or backend issue. Much likely you're not passing the appropriate payload to the backend. – kissu Commented Feb 5 at 15:12
- I tried Postman and it provided me such message "msg": "Subject must be a string". So I used toString to token but it provide me same error message 422. I also tried to add "" in Postman but by logs I see that token is received by api as None. – santeri pohjaranta Commented Feb 5 at 19:28
- One of theory could be that when token is send to api it recive none object instead of key and because of that Postman provide message "msg": "Subject must be a string". But why it change JWT key to none object? – santeri pohjaranta Commented Feb 5 at 19:45
- I tried toString for JWT in VUE. Same issue. – santeri pohjaranta Commented Feb 5 at 22:30
1 Answer
Reset to default 0I found problem. I did not added expire time for token. Because of that when I tried to send it to api it did not understood what it was.
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(days=7)
Another option why it was no working because I did not added required extension get_jwt
from flask_jwt_extended import JWTManager, create_access_token, jwt_required, get_jwt_identity, get_jwt