File size: 2,989 Bytes
68f3e34
 
 
 
d10d21b
 
 
68f3e34
 
f5ce5e6
a5cbc50
68f3e34
 
 
 
 
 
a5cbc50
35a90d3
a5cbc50
d10d21b
0f92878
a5cbc50
9f4281b
68f3e34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d10d21b
68f3e34
 
 
 
 
d10d21b
 
 
68f3e34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import requests
import gradio as gr
from urllib.parse import urlparse, parse_qs

import pre


def id_check(request: gr.Request):
    param = str(request.query_params)
    if not "code=" in param:
        return "Login to Discord", gr.Tab(visible=False), gr.Tab(visible=False), '', gr.Tab(visible=False), gr.Dropdown(info='Login to create preset')

    code = param.split('code=')
    print(code[1])

    user_id, username, avatar_url = get_discord_user_profile(str(code[1]))
    if user_id == None:
        return "Login to Discord", gr.Tab(visible=False), gr.Tab(visible=False), '', gr.Tab(visible=False), gr.Dropdown(info='Login to create preset')
    if user_id == "412827396027187211":
        return gr.Button(value=username, icon=avatar_url, link=redirect_uri), gr.Tab(visible=True), gr.Tab(visible=True), user_id, gr.Tab(visible=True), gr.Dropdown(info='')

    print(user_id)
    return gr.Button(value=username, icon=avatar_url, link=redirect_uri), gr.Tab(visible=False), gr.Tab(visible=False), user_id, gr.Tab(visible=True), gr.Dropdown(info='')
#1

def get_discord_user_profile(code):
    # Discord์˜ API ์—”๋“œํฌ์ธํŠธ
    token_url = "https://discord.com/api/oauth2/token"
    user_url = "https://discord.com/api/users/@me"

    try:
        # ํ† ํฐ ์š”์ฒญ์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ
        data = {
            'client_id': client_id,  # client_id ์„ค์ •
            'client_secret': client_secret,  # client_secret ์„ค์ •
            'grant_type': 'authorization_code',
            'code': code,
            'redirect_uri': redirect_uri  # redirect_uri ์„ค์ •
        }
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded'
        }

        # ์•ก์„ธ์Šค ํ† ํฐ ์š”์ฒญ
        token_response = requests.post(token_url, data=data, headers=headers)
        token_response.raise_for_status()  # ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ
        access_token = token_response.json().get('access_token')

        # ์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ
        auth_headers = {
            'Authorization': f'Bearer {access_token}'
        }
        user_response = requests.get(user_url, headers=auth_headers)
        user_response.raise_for_status()  # ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ ์˜ˆ์™ธ ๋ฐœ์ƒ
        user_info = user_response.json()

        user_id = user_info.get('id')
        username = user_info.get('username') + '#' + user_info.get('discriminator')
        avatar_hash = user_info.get('avatar')
        avatar_url = f"https://cdn.discordapp.com/avatars/{user_id}/{avatar_hash}.png" if avatar_hash else None

        return user_id, username, avatar_url

    except requests.exceptions.RequestException as e:
        print("Error to login")
        # print(str(e))  # ์˜ค๋ฅ˜ ๋‚ด์šฉ ์ถœ๋ ฅ์„ ์›ํ•œ๋‹ค๋ฉด ์ฃผ์„ ํ•ด์ œ
        return None, None, None

# ์‚ฌ์šฉ ์˜ˆ:
client_id = "1239473790732472370"
client_secret = "C7RLdB4YArSM9jVk8tsznQnKOzwGKwsO"
# code = "THE_CODE_FROM_REDIRECT_URI"
#redirect_uri = "https://chohj06ms-objektify2.hf.space/"
redirect_uri = pre.login_uri