1414 logger .error (f"GITHUB_CLIENT_SECRET: { 'present' if GITHUB_CLIENT_SECRET else 'missing' } " )
1515 raise ValueError ("❌ GitHub OAuth credentials not found" )
1616
17+ print (f"🔑 GitHub OAuth Configuration: Client ID: { GITHUB_CLIENT_ID [:5 ]} ..." )
18+
1719async def exchange_code_for_token (code : str ):
18- if not code :
19- raise HTTPException (status_code = 400 , detail = "GitHub code is required" )
20-
20+
21+ """Exchange GitHub OAuth code for token data"""
2122 try :
2223 async with httpx .AsyncClient () as client :
23- logger .info (f"Exchanging code for token with GitHub" )
24+ print (f"🚀 Exchanging code for token with GitHub..." )
25+ print (f"Using code: { code [:10 ]} ..." )
26+
27+ # Log credentials being used (partially obscured for security)
28+ print (f"🔑 Using Client ID: { GITHUB_CLIENT_ID [:5 ]} ..." )
29+ print (f"🔑 Using Client Secret: { GITHUB_CLIENT_SECRET [:5 ]} ..." )
30+
2431 response = await client .post (
2532 "https://github.com/login/oauth/access_token" ,
26- headers = {"Accept" : "application/json" },
33+ headers = {
34+ "Accept" : "application/json"
35+ },
36+
2737 data = {
2838 "client_id" : GITHUB_CLIENT_ID ,
2939 "client_secret" : GITHUB_CLIENT_SECRET ,
3040 "code" : code
3141 }
3242 )
33- response .raise_for_status ()
34- data = response .json ()
35- logger .info ("Successfully exchanged code for token" )
36- return data
37- except httpx .HTTPError as e :
38- logger .error (f"HTTP error during token exchange: { str (e )} " )
39- logger .error (f"Response status: { e .response .status_code if hasattr (e , 'response' ) else 'unknown' } " )
40- logger .error (f"Response body: { e .response .text if hasattr (e , 'response' ) else 'unknown' } " )
41- raise HTTPException (status_code = 500 , detail = f"GitHub API error: { str (e )} " )
43+
44+ print (f"✅ GitHub token exchange status: { response .status_code } " )
45+ print (f"Response headers: { dict (response .headers )} " )
46+
47+ try :
48+ response_text = response .text
49+ print (f"Raw response text: { response_text } " )
50+ data = response .json ()
51+ print (f"Parsed response data keys: { list (data .keys ())} " )
52+ except Exception as e :
53+ print (f"❌ Failed to parse JSON response: { response_text } " )
54+ raise Exception (f"Failed to parse GitHub response: { str (e )} " )
55+
56+ if "error" in data :
57+ error_description = data .get ("error_description" , "No description provided" )
58+ print (f"❌ GitHub OAuth error: { data ['error' ]} - { error_description } " )
59+ raise Exception (f"GitHub OAuth error: { data ['error' ]} - { error_description } " )
60+
61+ if "access_token" not in data :
62+ print ("❌ Access token is missing from response" )
63+ print (f"Available fields in response: { list (data .keys ())} " )
64+ raise Exception ("No access token in GitHub response" )
65+
66+ # Ensure token is properly stored and formatted
67+ access_token = data .get ("access_token" , "" ).strip ()
68+ if not access_token :
69+ print ("❌ Access token is empty" )
70+ raise Exception ("Empty access token received from GitHub" )
71+
72+ # Create a new clean data object to avoid any issues
73+ token_data = {
74+ "access_token" : access_token ,
75+ "token_type" : data .get ("token_type" , "bearer" ),
76+ "scope" : data .get ("scope" , "" )
77+ }
78+
79+ print (f"🔑 Successfully obtained token data with fields: { list (token_data .keys ())} " )
80+ print (f"🔑 Access token value: { access_token [:10 ]} ..." )
81+
82+ # Directly log the full token JUST FOR DEBUGGING (would remove in production)
83+ print (f"🔑 FULL TOKEN FOR DEBUG: { access_token } " )
84+
85+ # Verify token data is correctly formed
86+ if not token_data .get ("access_token" ):
87+ print ("⚠️ WARNING: access_token field is empty or missing in final token_data" )
88+ # Try once more to ensure it's set
89+ token_data ["access_token" ] = access_token
90+
91+ return token_data
92+
4293 except Exception as e :
43- logger . exception ( " Error exchanging code for token: " )
44- raise HTTPException ( status_code = 500 , detail = str ( e ))
94+ print ( f"🔥 Error in token exchange: { str ( e ) } " )
95+ raise
4596
4697async def get_user_info (access_token : str ):
47- if not access_token :
48- raise HTTPException (status_code = 400 , detail = "Access token is required" )
49-
98+ """Get GitHub user information using access token"""
5099 try :
51100 async with httpx .AsyncClient () as client :
52- logger .info ("Fetching user info from GitHub" )
101+ print (f"👤 Fetching user info from GitHub..." )
102+ print (f"👤 Using token: { access_token [:10 ]} ..." )
103+
104+ # Try both authentication methods
105+ headers = {
106+ "Authorization" : f"token { access_token } " , # GitHub preferred format
107+ "Accept" : "application/vnd.github.v3+json" ,
108+ "User-Agent" : "LIT1337-App"
109+ }
110+
53111 response = await client .get (
54112 "https://api.github.com/user" ,
55- headers = { "Authorization" : f"Bearer { access_token } " }
113+ headers = headers
56114 )
57- response .raise_for_status ()
115+
116+ if response .status_code != 200 :
117+ # Try alternative Bearer format if token format fails
118+ print (f"First attempt failed with status { response .status_code } , trying Bearer format" )
119+ alt_headers = {
120+ "Authorization" : f"Bearer { access_token } " , # OAuth standard
121+ "Accept" : "application/vnd.github.v3+json" ,
122+ "User-Agent" : "LIT1337-App"
123+ }
124+
125+ response = await client .get (
126+ "https://api.github.com/user" ,
127+ headers = alt_headers
128+ )
129+
130+ print (f"GitHub API response status: { response .status_code } " )
131+
132+ if response .status_code != 200 :
133+ error_data = response .json ()
134+ print (f"❌ GitHub API error response: { error_data } " )
135+ raise Exception (f"GitHub API error: { response .status_code } - { error_data .get ('message' , 'Unknown error' )} " )
136+
58137 data = response .json ()
59- logger . info ( "Successfully fetched user info" )
138+ print ( f"✅ GitHub user info received for: { data . get ( 'login' ) } " )
60139 return data
61- except httpx .HTTPError as e :
62- logger .error (f"HTTP error fetching user info: { str (e )} " )
63- logger .error (f"Response status: { e .response .status_code if hasattr (e , 'response' ) else 'unknown' } " )
64- logger .error (f"Response body: { e .response .text if hasattr (e , 'response' ) else 'unknown' } " )
65- raise HTTPException (status_code = 500 , detail = f"GitHub API error: { str (e )} " )
140+
66141 except Exception as e :
67- logger . exception ( " Error fetching user info:" )
68- raise HTTPException ( status_code = 500 , detail = str ( e ))
142+ print ( f"❌ Error getting user info: { str ( e ) } " )
143+ raise
0 commit comments