Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -187,30 +187,33 @@ async def oauth_callback(request: Request):
|
|
| 187 |
if not (OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET):
|
| 188 |
return RedirectResponse("/?login_error=server_unconfigured")
|
| 189 |
|
| 190 |
-
|
|
|
|
|
|
|
| 191 |
try:
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
| 214 |
if not me_resp.is_success:
|
| 215 |
return RedirectResponse("/?login_error=whoami")
|
| 216 |
me = me_resp.json()
|
|
|
|
| 187 |
if not (OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET):
|
| 188 |
return RedirectResponse("/?login_error=server_unconfigured")
|
| 189 |
|
| 190 |
+
# Use a fresh client so we don't inherit `Authorization: Bearer HF_TOKEN`
|
| 191 |
+
# from app.state.client — HF's /oauth/token expects client_id+client_secret,
|
| 192 |
+
# not a Space-token Bearer header, and rejects the request otherwise.
|
| 193 |
try:
|
| 194 |
+
async with httpx.AsyncClient(timeout=httpx.Timeout(HUB_FETCH_TIMEOUT), follow_redirects=True) as oauth_client:
|
| 195 |
+
token_resp = await oauth_client.post(
|
| 196 |
+
f"{HUB}/oauth/token",
|
| 197 |
+
data={
|
| 198 |
+
"grant_type": "authorization_code",
|
| 199 |
+
"code": code,
|
| 200 |
+
"redirect_uri": _redirect_uri(request),
|
| 201 |
+
"client_id": OAUTH_CLIENT_ID,
|
| 202 |
+
"client_secret": OAUTH_CLIENT_SECRET,
|
| 203 |
+
},
|
| 204 |
+
headers={"Accept": "application/json"},
|
| 205 |
+
)
|
| 206 |
+
if not token_resp.is_success:
|
| 207 |
+
log.warning("OAuth token exchange failed: %s %s", token_resp.status_code, token_resp.text[:200])
|
| 208 |
+
return RedirectResponse("/?login_error=token_exchange")
|
| 209 |
+
access_token = token_resp.json().get("access_token")
|
| 210 |
+
if not access_token:
|
| 211 |
+
return RedirectResponse("/?login_error=no_token")
|
| 212 |
+
|
| 213 |
+
me_resp = await oauth_client.get(
|
| 214 |
+
f"{HUB}/api/whoami-v2",
|
| 215 |
+
headers={"Authorization": f"Bearer {access_token}"},
|
| 216 |
+
)
|
| 217 |
if not me_resp.is_success:
|
| 218 |
return RedirectResponse("/?login_error=whoami")
|
| 219 |
me = me_resp.json()
|