Just tried the latest okta-flask: GitHub - okta-samples/okta-flask-sample: Python Flask + Okta - but I am getting an error: KeyError: ‘app_state’
from app.py
@app.route(“/authorization-code/callback”)
def callback():
headers = {‘Content-Type’: ‘application/x-www-form-urlencoded’}
code = request.args.get(“code”)
# print(code)
app_state = request.args.get(“state”)
# print(app_state)
# print(session[‘app_state’])
if app_state != session[‘app_state’]:
return “The app state does not match”
if not code:
return “The code was not returned or is not accessible”, 403
query_params = {‘grant_type’: ‘authorization_code’,
‘code’: code,
‘redirect_uri’: request.base_url,
‘code_verifier’: session[‘code_verifier’],
}
query_params = requests.compat.urlencode(query_params)
exchange = requests.post(
os.environ[‘ORG_URL’] + “oauth2/default/v1/token”,
headers=headers,
data=query_params,
auth=(os.environ[‘CLIENT_ID’], os.environ[‘CLIENT_SECRET’]),
).json()
# Get tokens and validate
if not exchange.get("token_type"):
return "Unsupported token type. Should be 'Bearer'.", 403
access_token = exchange["access_token"]
id_token = exchange["id_token"]
# Authorization flow successful, get userinfo and login user
userinfo_response = requests.get(os.environ['ORG_URL'] + "oauth2/default/v1/userinfo",
headers={'Authorization': f'Bearer {access_token}'}).json()
unique_id = userinfo_response["sub"]
user_email = userinfo_response["email"]
user_name = userinfo_response["given_name"]
user = User(
id_=unique_id, name=user_name, email=user_email
)
if not User.get(unique_id):
User.create(unique_id, user_name, user_email)
login_user(user)
return redirect(url_for("profile"))
This is what I am getting:
Traceback (most recent call last):
File “/Users/xxxx/PycharmProjects/okta-flask-sample/venv/lib/python3.7/site-packages/flask/app.py”, line 2073, in wsgi_app
response = self.full_dispatch_request()
File “/Users/xxxxxx/PycharmProjects/okta-flask-sample/venv/lib/python3.7/site-packages/flask/app.py”, line 1518, in full_dispatch_request
rv = self.handle_user_exception(e)
File “/Users/xxxxxx/PycharmProjects/okta-flask-sample/venv/lib/python3.7/site-packages/flask_cors/extension.py”, line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File “/Users/jxxxxxx/PycharmProjects/okta-flask-sample/venv/lib/python3.7/site-packages/flask/app.py”, line 1516, in full_dispatch_request
rv = self.dispatch_request()
File “/Users/xxxxxxx/PycharmProjects/okta-flask-sample/venv/lib/python3.7/site-packages/flask/app.py”, line 1502, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File “/Users/xxx/PycharmProjects/okta-flask-sample/app.py”, line 83, in callback
print(session[‘app_state’])
File “/Users/xxxxxxxxxx/PycharmProjects/okta-flask-sample/venv/lib/python3.7/site-packages/flask/sessions.py”, line 79, in getitem
return super().getitem(key)
KeyError: ‘app_state’
127.0.0.1 - - [05/Sep/2023 12:51:39] “GET /authorization-code/callback?code=xxxxxxxxxxxxxxxx HTTP/1.1” 500 -
when I print( session[“app_state”]) - I get an
GET error on the route: ERROR in app: “Exception on /authorization-code/callback [GET]”
How do I get around this?