DBFahrerKaterchen3 commited on
Commit
cf9d368
·
verified ·
1 Parent(s): 3e4684e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -49
app.py CHANGED
@@ -1,24 +1,17 @@
1
  import os
2
  import urllib.parse
3
  from fastapi import FastAPI, HTTPException
4
- from fastapi.responses import HTMLResponse, RedirectResponse
5
- import requests
6
- import urllib3
7
- urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
8
 
9
  app = FastAPI()
10
 
11
  CLIENT_ID = os.getenv("DISCORD_CLIENT_ID")
12
  CLIENT_SECRET = os.getenv("DISCORD_CLIENT_SECRET")
13
-
14
- # WICHTIG: Passe diese URL an deinen HF-Namen und Space-Namen an!
15
- # Format: https://username-space-name.hf.space/oauth/callback
16
- # Achtung: Bindestriche statt Slashes im Domain-Namen!
17
  REDIRECT_URI = "https://rsbtech-rsbtech-verification.hf.space/oauth/callback"
18
 
19
  @app.get("/", response_class=HTMLResponse)
20
  async def home():
21
- # Der Link, der den Nutzer zu Discord schickt, um die App zu autorisieren
22
  scopes = "identify role_connections.write"
23
  encoded_redirect = urllib.parse.quote(REDIRECT_URI, safe='')
24
 
@@ -47,7 +40,6 @@ async def oauth_callback(code: str = None):
47
  if not code:
48
  raise HTTPException(status_code=400, detail="Kein Code vorhanden.")
49
 
50
- # 1. Code gegen Token tauschen
51
  token_url = "https://discord.com/api/v10/oauth2/token"
52
  data = {
53
  "client_id": CLIENT_ID,
@@ -58,45 +50,49 @@ async def oauth_callback(code: str = None):
58
  }
59
  headers = {"Content-Type": "application/x-www-form-urlencoded"}
60
 
61
- # Füge 'verify=False' am Ende hinzu, um den SSL-Handshake-Absturz zu umgehen
62
- token_res = requests.post(token_url, data=data, headers=headers, verify=False)
63
- token_data = token_res.json()
64
-
65
- if "access_token" not in token_data:
66
- return HTMLResponse(f"<h3 style='color:red;'>Fehler beim Login: {token_data}</h3>")
67
-
68
- access_token = token_data["access_token"]
 
 
 
69
 
70
- # 2. Nutzerdaten holen (um den Namen anzuzeigen)
71
- user_res = requests.get("https://discord.com/api/v10/users/@me", headers={
72
- "Authorization": f"Bearer {access_token}"
73
- })
74
- user_json = user_res.json()
75
- username = user_json.get("username", "Nutzer")
76
 
77
- # 3. Linked Roles Metadaten an Discord übermitteln
78
- # Hier sagen wir Discord, dass 'is_verified' für diesen Nutzer nun True (1) ist
79
- connection_url = f"https://discord.com/api/v10/users/@me/applications/{CLIENT_ID}/role-connection"
80
-
81
- connection_body = {
82
- "platform_name": "HF Space Anmeldung",
83
- "platform_username": username,
84
- "metadata": {
85
- "is_verified": 1 # 1 steht für True
86
- }
87
- }
88
-
89
- put_res = requests.put(connection_url, json=connection_body, headers={
90
- "Authorization": f"Bearer {access_token}",
91
- "Content-Type": "application/json"
92
- })
93
 
94
- if put_res.status_code == 200:
95
- return HTMLResponse(f"""
96
- <body style="font-family: Arial; text-align: center; padding-top: 100px; background-color: #23272A; color: white;">
97
- <h2 style="color: #57F287;">Erfolgreich verbunden, {username}!</h2>
98
- <p>Du kannst dieses Fenster jetzt schließen und deine Rolle auf Discord abholen.</p>
99
- </body>
100
- """)
101
- else:
102
- return HTMLResponse(f"<h3 style='color:red;'>Metadaten-Fehler: {put_res.text}</h3>")
 
 
 
 
1
  import os
2
  import urllib.parse
3
  from fastapi import FastAPI, HTTPException
4
+ from fastapi.responses import HTMLResponse
5
+ import httpx
 
 
6
 
7
  app = FastAPI()
8
 
9
  CLIENT_ID = os.getenv("DISCORD_CLIENT_ID")
10
  CLIENT_SECRET = os.getenv("DISCORD_CLIENT_SECRET")
 
 
 
 
11
  REDIRECT_URI = "https://rsbtech-rsbtech-verification.hf.space/oauth/callback"
12
 
13
  @app.get("/", response_class=HTMLResponse)
14
  async def home():
 
15
  scopes = "identify role_connections.write"
16
  encoded_redirect = urllib.parse.quote(REDIRECT_URI, safe='')
17
 
 
40
  if not code:
41
  raise HTTPException(status_code=400, detail="Kein Code vorhanden.")
42
 
 
43
  token_url = "https://discord.com/api/v10/oauth2/token"
44
  data = {
45
  "client_id": CLIENT_ID,
 
50
  }
51
  headers = {"Content-Type": "application/x-www-form-urlencoded"}
52
 
53
+ # Wir nutzen httpx mit deaktivierter SSL-Prüfung (verify=False), falls die Container-Zertifikate spinnen
54
+ async with httpx.AsyncClient(verify=False) as client:
55
+ try:
56
+ # 1. Code gegen Token tauschen
57
+ token_res = await client.post(token_url, data=data, headers=headers)
58
+ token_data = token_res.json()
59
+
60
+ if "access_token" not in token_data:
61
+ return HTMLResponse(f"<h3 style='color:red;'>Discord Fehler beim Token-Austausch: {token_data}</h3>")
62
+
63
+ access_token = token_data["access_token"]
64
 
65
+ # 2. Nutzerdaten holen
66
+ user_res = await client.get("https://discord.com/api/v10/users/@me", headers={
67
+ "Authorization": f"Bearer {access_token}"
68
+ })
69
+ user_json = user_res.json()
70
+ username = user_json.get("username", "Nutzer")
71
 
72
+ # 3. Linked Roles Metadaten an Discord übermitteln
73
+ connection_url = f"https://discord.com/api/v10/users/@me/applications/{CLIENT_ID}/role-connection"
74
+ connection_body = {
75
+ "platform_name": "HF Space Anmeldung",
76
+ "platform_username": username,
77
+ "metadata": {
78
+ "is_verified": 1
79
+ }
80
+ }
81
+
82
+ put_res = await client.put(connection_url, json=connection_body, headers={
83
+ "Authorization": f"Bearer {access_token}",
84
+ "Content-Type": "application/json"
85
+ })
 
 
86
 
87
+ if put_res.status_code == 200:
88
+ return HTMLResponse(f"""
89
+ <body style="font-family: Arial; text-align: center; padding-top: 100px; background-color: #23272A; color: white;">
90
+ <h2 style="color: #57F287;">Erfolgreich verbunden, {username}!</h2>
91
+ <p>Du kannst dieses Fenster jetzt schließen und deine Rolle auf Discord abholen.</p>
92
+ </body>
93
+ """)
94
+ else:
95
+ return HTMLResponse(f"<h3 style='color:red;'>Metadaten-Fehler: {put_res.text}</h3>")
96
+
97
+ except Exception as e:
98
+ return HTMLResponse(f"<h3 style='color:red;'>Netzwerk-Fehler im Space-Backend: {str(e)}</h3>")