Я получаю эту ошибку после нажатия кнопки «Войти»:
Код: Выделить всё
{ "error": "Failed to exchange token" }
Вот весь мой код Auth0 (python):
Код: Выделить всё
@app.route('/callback')
def callback():
state = request.args.get('state')
code = request.args.get('code')
print(f"Callback URL: {request.url}")
print(f"State in session: {session.get('oauth_state')}")
print(f"State in request: {state}")
print(f"Code received: {code}")
# Verify that the state matches
if state != session.get('oauth_state'):
print("State does not match! CSRF Warning!")
return jsonify({"error": "Invalid state parameter"}), 403
# Exchange the authorization code for an access token
try:
token = oauth.auth0.authorize_access_token()
except Exception as e:
print(f"Error during token exchange: {str(e)}")
return jsonify({"error": "Failed to exchange token"}), 500
# Get user information
user_info = get_user_info(token['access_token'])
if user_info:
session['user'] = user_info # Store user info in session
session.pop('oauth_state', None) # Clear state
return redirect('/dashboard') # Redirect to the dashboard
else:
return jsonify({"error": "Failed to fetch user info"}), 500
def get_user_info(access_token):
url = f'https://{AUTH0_DOMAIN}/userinfo'
headers = {
'Authorization': f'Bearer {access_token}'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return response.json() # Return user info as a dictionary
return None
@app.route('/dashboard')
def dashboard():
user_info = session.get('user') # Retrieve user info from the session
if user_info:
return render_template('dashboard.html', user=user_info) # Render the dashboard with user info
else:
return redirect(url_for('login')) # Redirect if not authenticated
@app.route('/logout')
def logout():
session.clear()
return redirect(f'https://{AUTH0_DOMAIN}/v2/logout?returnTo={url_for("index", _external=True)}&client_id={CLIENT_ID}')
После нескольких запросов даже ChatGPT может Не помогу!
Я был бы очень признателен за помощь, потому что это заняло некоторое время.
Обычно я разрабатываю интерфейсную часть и от В ходе своего исследования я пришел к выводу, что я, вероятно, единственный, кто получает эту ошибку, лол.
Подробнее здесь: https://stackoverflow.com/questions/791 ... hange-toke