Im building an app in android using flask and jwt; how do i pass a token to a route? This is my login:
@app.route('/login', methods=['POST'])
def login_user():
user_info = request.get_json()
user_obj = User.query.filter_by(username = user_info['username']).first()
if user_obj is not None:
if user_info['password'] == user_obj.password:
token = create_access_token(identity = user_obj.username)
return jsonify({'access_token':token})
and my protected route:
@app.route('/jwt/users', methods = ['GET'])
@jwt_required
def get_all_users():
current_user = get_jwt_identity()
return jsonify({"result": current_user})
Now how do i pass the token created in my login to the protected route without postman? Im using android studio as a client.
This is my app.config:
app.config['JWT_TOKEN_LOCATION'] = ['headers', 'query_string']
app.config['JWT_SECRET_KEY'] = 'this-is-the-secret-key'
(will obv change secret key later)
Pass the token as part of the header
to your protected endpoint.
Look at the example here
$ curl http://localhost:5000/protected
{
"msg": "Missing Authorization Header"
}
$ curl -H "Content-Type: application/json" -X POST \
-d '{"username":"test","password":"test"}' http://localhost:5000/login
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6dHJ1ZSwianRpIjoiZjhmNDlmMjUtNTQ4OS00NmRjLTkyOWUtZTU2Y2QxOGZhNzRlIiwidXNlcl9jbGFpbXMiOnt9LCJuYmYiOjE0NzQ0NzQ3OTEsImlhdCI6MTQ3NDQ3NDc5MSwiaWRlbnRpdHkiOiJ0ZXN0IiwiZXhwIjoxNDc0NDc1NjkxLCJ0eXBlIjoiYWNjZXNzIn0.vCy0Sec61i9prcGIRRCbG8e9NV6_wFH2ICFgUGCLKpc"
}
$ export ACCESS="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6dHJ1ZSwianRpIjoiZjhmNDlmMjUtNTQ4OS00NmRjLTkyOWUtZTU2Y2QxOGZhNzRlIiwidXNlcl9jbGFpbXMiOnt9LCJuYmYiOjE0NzQ0NzQ3OTEsImlhdCI6MTQ3NDQ3NDc5MSwiaWRlbnRpdHkiOiJ0ZXN0IiwiZXhwIjoxNDc0NDc1NjkxLCJ0eXBlIjoiYWNjZXNzIn0.vCy0Sec61i9prcGIRRCbG8e9NV6_wFH2ICFgUGCLKpc"
$ curl -H "Authorization: Bearer $ACCESS" http://localhost:5000/protected
{
"logged_in_as": "test"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.