"Not enough segments" when seding a GET message with Bearer Token Authorization Header (flask_restful + flask_jwt_extended)
The token you are trying to pass in (TGazPL9rf3aIftplCYDTGDc8cbTd
)is not a valid JWT. A valid JWT has three segments separated by dots: <base64_encoded_header>.<base64_encoded_payload>.<signature>
. You can read more about it here: https://jwt.io/introduction/
I will post here an answer related to my initial problem above, the context is that I was trying to user flask_jwt_extend to use in firebase authentication, but I have this "Not enough segments" errors and I got blocked.
So after that, I change my code to:
from flask import Flask, requestfrom flask_restful import Resource, Apifrom functools import wrapsimport google.auth.transport.requestsimport google.oauth2.id_tokenapp = Flask(__name__)api = Api(app)HTTP_REQUEST = google.auth.transport.requests.Request()def jwt_required_gcp(fn): @wraps(fn) def wrapper(*args, **kwargs): id_token = request.headers['Authorization'].split(' ').pop() claims = google.oauth2.id_token.verify_firebase_token( id_token, HTTP_REQUEST) if not claims: return 'Unauthorized', 401 return fn(*args, **kwargs) return wrapperclass HelloWorld(Resource): @jwt_required_gcp def get(self): return {'hello': 'world'}api.add_resource(HelloWorld, '/')
Check your JWT Token. Is it valid?
@fresh_jwt_required - fresh_jwt_required() function to only allow fresh tokens to access the certain endpoint
@jwt_required - A decorator to protect a Flask endpoint with JSON Web Tokens. Any route decorated with this will require a valid JWT to be present in the request (unless optional=True, in which case no JWT is also valid) before the endpoint can be called.
For more detail review flask-jwt-extended