from flask import Flask, request, redirect, session, url_for import requests import os app = Flask(__name__) app.secret_key = 'YOUR_SECRET_KEY' # Замените на ваш секретный ключ def get_access_token(code): client_id = 'YOUR_APP_ID' client_secret = 'YOUR_APP_SECRET' redirect_uri = 'https://example.com/login' url = f'https://oauth.vk.com/access_token?client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}&code={code}' response = requests.get(url) data = response.json() if 'access_token' in data: access_token = data['access_token'] user_id = data['user_id'] return access_token, user_id else: return None, None @app.route('/login') def login(): code = request.args.get('code') if code: access_token, user_id = get_access_token(code) if access_token and user_id: session['access_token'] = access_token session['user_id'] = user_id return redirect(url_for('protected_route')) else: return "Ошибка при получении access_token", 400 else: return "Код авторизации не найден", 400 def is_authenticated(): return 'access_token' in session and 'user_id' in session @app.route('/protected') def protected_route(): if is_authenticated(): user_id = session['user_id'] return f"Добро пожаловать, пользователь с ID {user_id}!" else: return redirect(url_for('login')) if __name__ == '__main__': app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))