|
from flask import Flask, request, redirect, session |
|
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('/') |
|
else: |
|
return "Ошибка при получении access_token", 400 |
|
else: |
|
return "Код авторизации не найден", 400 |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860))) |