This commit is contained in:
@@ -233,30 +233,45 @@ PROVIDER_HANDLERS = {
|
|||||||
async def _fetch_github_profile(client: Any, token: Any) -> dict:
|
async def _fetch_github_profile(client: Any, token: Any) -> dict:
|
||||||
"""Получает профиль из GitHub API"""
|
"""Получает профиль из GitHub API"""
|
||||||
try:
|
try:
|
||||||
# Получаем основной профиль
|
# Извлекаем access_token из ответа
|
||||||
profile = await client.get("user", token=token)
|
access_token = token.get("access_token") if isinstance(token, dict) else token
|
||||||
profile_data = profile.json()
|
|
||||||
|
|
||||||
# Проверяем наличие ошибок в ответе GitHub
|
if not access_token:
|
||||||
if "message" in profile_data:
|
logger.error("No access_token found in GitHub token response")
|
||||||
logger.error(f"GitHub API error: {profile_data['message']}")
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
# Получаем email адреса (требует scope user:email)
|
# Используем прямой HTTP запрос к GitHub API
|
||||||
emails = await client.get("user/emails", token=token)
|
headers = {
|
||||||
emails_data = emails.json()
|
"Authorization": f"Bearer {access_token}",
|
||||||
|
"Accept": "application/vnd.github.v3+json",
|
||||||
# Ищем основной email
|
"User-Agent": "Discours-OAuth-Client",
|
||||||
primary_email = None
|
|
||||||
if isinstance(emails_data, list):
|
|
||||||
primary_email = next((email["email"] for email in emails_data if email.get("primary")), None)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"id": str(profile_data.get("id", "")),
|
|
||||||
"email": primary_email or profile_data.get("email"),
|
|
||||||
"name": profile_data.get("name") or profile_data.get("login", ""),
|
|
||||||
"picture": profile_data.get("avatar_url"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async with httpx.AsyncClient() as http_client:
|
||||||
|
# Получаем основной профиль
|
||||||
|
profile_response = await http_client.get("https://api.github.com/user", headers=headers)
|
||||||
|
|
||||||
|
if profile_response.status_code != 200:
|
||||||
|
logger.error(f"GitHub API error: {profile_response.status_code} - {profile_response.text}")
|
||||||
|
return {}
|
||||||
|
|
||||||
|
profile_data = profile_response.json()
|
||||||
|
|
||||||
|
# Получаем email адреса (требует scope user:email)
|
||||||
|
emails_response = await http_client.get("https://api.github.com/user/emails", headers=headers)
|
||||||
|
emails_data = emails_response.json() if emails_response.status_code == 200 else []
|
||||||
|
|
||||||
|
# Ищем основной email
|
||||||
|
primary_email = None
|
||||||
|
if isinstance(emails_data, list):
|
||||||
|
primary_email = next((email["email"] for email in emails_data if email.get("primary")), None)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"id": str(profile_data.get("id", "")),
|
||||||
|
"email": primary_email or profile_data.get("email"),
|
||||||
|
"name": profile_data.get("name") or profile_data.get("login", ""),
|
||||||
|
"picture": profile_data.get("avatar_url"),
|
||||||
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error fetching GitHub profile: {e}")
|
logger.error(f"Error fetching GitHub profile: {e}")
|
||||||
return {}
|
return {}
|
||||||
|
|||||||
Reference in New Issue
Block a user