Spaces:
Sleeping
Sleeping
thomasgauthier
commited on
Commit
·
8b05b02
1
Parent(s):
b14eb2f
get username
Browse files
main.py
CHANGED
@@ -392,7 +392,8 @@ async def oauth_callback(code: str, state: str):
|
|
392 |
credentials_bytes = credentials.encode("ascii")
|
393 |
base64_credentials = base64.b64encode(credentials_bytes)
|
394 |
auth_header = f"Basic {base64_credentials.decode('ascii')}"
|
395 |
-
|
|
|
396 |
try:
|
397 |
token_response = requests.post(
|
398 |
'https://huggingface.co/oauth/token',
|
@@ -409,6 +410,30 @@ async def oauth_callback(code: str, state: str):
|
|
409 |
if token_response.status_code == 200:
|
410 |
tokens = token_response.json()
|
411 |
access_token = tokens.get('access_token')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
# ID Token can be extracted here if needed
|
413 |
# id_token = tokens.get('id_token')
|
414 |
else:
|
@@ -418,7 +443,7 @@ async def oauth_callback(code: str, state: str):
|
|
418 |
traceback.print_exc()
|
419 |
access_token = ""
|
420 |
|
421 |
-
return {"access_token": access_token}
|
422 |
|
423 |
@app.get("/oauth-config")
|
424 |
async def get_oauth_config(request: Request):
|
|
|
392 |
credentials_bytes = credentials.encode("ascii")
|
393 |
base64_credentials = base64.b64encode(credentials_bytes)
|
394 |
auth_header = f"Basic {base64_credentials.decode('ascii')}"
|
395 |
+
username = ""
|
396 |
+
|
397 |
try:
|
398 |
token_response = requests.post(
|
399 |
'https://huggingface.co/oauth/token',
|
|
|
410 |
if token_response.status_code == 200:
|
411 |
tokens = token_response.json()
|
412 |
access_token = tokens.get('access_token')
|
413 |
+
|
414 |
+
|
415 |
+
if access_token:
|
416 |
+
try:
|
417 |
+
user_response = requests.get(
|
418 |
+
'https://huggingface.co/api/user',
|
419 |
+
headers={'Authorization': f'Bearer {access_token}'}
|
420 |
+
)
|
421 |
+
|
422 |
+
if user_response.status_code == 200:
|
423 |
+
user_data = user_response.json()
|
424 |
+
username = user_data['username']
|
425 |
+
# Now you have the username, you can handle it as needed
|
426 |
+
else:
|
427 |
+
username = ""
|
428 |
+
print(f"Error getting user data: {user_response.status_code}, {user_response.text}")
|
429 |
+
|
430 |
+
except Exception:
|
431 |
+
traceback.print_exc()
|
432 |
+
username = ""
|
433 |
+
else:
|
434 |
+
username = ""
|
435 |
+
|
436 |
+
|
437 |
# ID Token can be extracted here if needed
|
438 |
# id_token = tokens.get('id_token')
|
439 |
else:
|
|
|
443 |
traceback.print_exc()
|
444 |
access_token = ""
|
445 |
|
446 |
+
return {"access_token": access_token, "username" : username}
|
447 |
|
448 |
@app.get("/oauth-config")
|
449 |
async def get_oauth_config(request: Request):
|