简体   繁体   中英

How to pass bearer token in url

I am having an ml rest API and I am generating a JWT token for it, now I want to test the API using the generated JWT token in the URL. How can I do this?

response = {}
@app.route('/predict', methods=["POST", "GET"])
@token_required
def predict():    
    if request.method=='POST':
        solute = request.form["solute"]
        solvent = request.form["solvent"]
    else:
        solute = request.args.get("solute")
        solvent = request.args.get("solvent")
    results = predictions(solute, solvent)
    print(results)
    response["predictions"] = results[0].item()
    response["interaction_map"] = (results[1].detach().numpy()).tolist()
    return flask.jsonify({'result': response})

def token_required(func):
    # decorator factory which invoks update_wrapper() method and passes decorated function as an argument
    @wraps(func)
    def decorated(*args, **kwargs):
        token = request.args.get('token')
        if not token:
            return jsonify({'Alert!': 'Token is missing!'}), 401

        try:

            data = jwt.decode(token, app.config['SECRET_KEY'])
        # You can use the JWT errors in exception
        # except jwt.InvalidTokenError:
        #     return 'Invalid token. Please log in again.'
        except:
            return jsonify({'Message': 'Invalid token'}), 403
        return func(*args, **kwargs)
    return decorated

This is the prediction part of where it needs the token to show the prediction results.

http://127.0.0.1:3000/predict?solute=CC(C)(C)Br&solvent=CC(C)(C)Br

I am sending my two query parameters as shown in the above URL to get the predictions. Now I want to know where should I have to add the JWT token to this URL

在此处输入图像描述

在此处输入图像描述

Based on the code you've posted, you're looking for a parameter named token :

token = request.args.get('token')

You appear to be passing access_token in your example, which sounds like the source of your problem.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM