-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagora_token_server.py
More file actions
53 lines (40 loc) · 1.35 KB
/
agora_token_server.py
File metadata and controls
53 lines (40 loc) · 1.35 KB
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
import os
import hashlib
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from agora_token_builder import RtcTokenBuilder
app = FastAPI()
class TokenRequest(BaseModel):
appId: str
appCertificate: str | None = None
channelName: str
uid: int
expireSeconds: int = 3600
@app.get("/health")
async def health():
return {"status": "ok"}
@app.post("/token")
async def create_token(req: TokenRequest):
app_id = req.appId or os.getenv("AGORA_APP_ID", "")
app_cert = req.appCertificate or os.getenv("AGORA_APP_CERT", "")
if not app_id or not app_cert:
raise HTTPException(status_code=400, detail="Missing App ID or App Certificate")
try:
# Role 1 = publisher in agora-token-builder defaults
token = RtcTokenBuilder.buildTokenWithUid(
app_id,
app_cert,
req.channelName,
req.uid,
1,
req.expireSeconds,
)
return {"token": token}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Failed to build token: {e}")
def generate_deterministic_uid_from_string(value: str) -> int:
# Produce a stable 32-bit unsigned int within Agora UID range
digest = hashlib.sha256(value.encode()).digest()
uid = int.from_bytes(digest[:4], "big")
# Avoid zero UID
return uid or 1