diff --git a/Sources/Services/Feishu/FeishuAuthService.swift b/Sources/Services/Feishu/FeishuAuthService.swift index ff45cd4..13b3361 100644 --- a/Sources/Services/Feishu/FeishuAuthService.swift +++ b/Sources/Services/Feishu/FeishuAuthService.swift @@ -42,20 +42,20 @@ final class FeishuAuthService { } func exchangeCode(_ code: String) async throws -> OAuthTokenData { - BuggerLog.info("exchangeCode: start, code len=\(code.count)") + print("[BUGGER] exchangeCode: start, code len=\(code.count)") let tenantToken = try await tenantAccessToken() - BuggerLog.info("exchangeCode: got tenant token, building request...") + print("[BUGGER] exchangeCode: got tenantToken=\(tenantToken.prefix(8))..., building request...") let url = URL(string: "\(baseURL)/authen/v1/oidc/access_token")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") request.setValue("Bearer \(tenantToken)", forHTTPHeaderField: "Authorization") - BuggerLog.info("exchangeCode: encoding body...") - let body: [String: String] = ["grant_type": "authorization_code", "code": code] - request.httpBody = try JSONEncoder().encode(body) - BuggerLog.info("exchangeCode: body encoded (\(request.httpBody?.count ?? 0)B), sending...") + // Use manual JSON to rule out JSONEncoder issues + let jsonBody = "{\"grant_type\":\"authorization_code\",\"code\":\"\(code)\"}" + request.httpBody = jsonBody.data(using: .utf8) + print("[BUGGER] exchangeCode: sending to \(url.absoluteString)...") let tokenData: OAuthTokenData = try await send(request) - BuggerLog.info("exchangeCode: got token access=\(tokenData.accessToken.prefix(8))... refresh=\(tokenData.refreshToken?.prefix(8) ?? "nil")...") + print("[BUGGER] exchangeCode: SUCCESS access=\(tokenData.accessToken.prefix(8))... refresh=\(tokenData.refreshToken?.prefix(8) ?? "nil")...") return tokenData } @@ -118,35 +118,32 @@ final class FeishuAuthService { } private func send(_ request: URLRequest) async throws -> T { - BuggerLog.info("→ \(request.httpMethod ?? "?") \(request.url?.absoluteString ?? "?")") + print("[BUGGER] → \(request.httpMethod ?? "?") \(request.url?.absoluteString ?? "?")") if let body = request.httpBody, let bodyStr = String(data: body, encoding: .utf8) { - BuggerLog.info(" body: \(bodyStr)") - } - if let auth = request.value(forHTTPHeaderField: "Authorization") { - BuggerLog.info(" auth: \(auth.prefix(20))...") + print("[BUGGER] body: \(bodyStr)") } let (data, response) = try await session.data(for: request) guard let httpResponse = response as? HTTPURLResponse else { - BuggerLog.error("← not an HTTP response") + print("[BUGGER] ← not an HTTP response") throw FeishuError.networkError(nil) } let bodyStr = String(data: data, encoding: .utf8) ?? "" - BuggerLog.info("← \(httpResponse.statusCode) \(bodyStr.prefix(500))") + print("[BUGGER] ← \(httpResponse.statusCode) \(bodyStr.prefix(500))") if httpResponse.statusCode == 401 { - BuggerLog.error("← 401 Unauthorized") + print("[BUGGER] ← 401 Unauthorized") throw FeishuError.unauthorized } do { let decoded = try decoder.decode(T.self, from: data) - BuggerLog.info(" decoded as \(T.self) ✓") + print("[BUGGER] decoded as \(T.self) ✓") return decoded } catch { - BuggerLog.error(" decode failed: \(error.localizedDescription)") - BuggerLog.error(" raw body: \(bodyStr.prefix(1000))") + print("[BUGGER] decode FAILED: \(error)") + print("[BUGGER] raw body: \(bodyStr.prefix(1000))") throw FeishuError.decodingError(error) } }