this repo has no description

Add debug output and improved token handling

- Added detailed HTTP debugging to print request/response details
- Improved user guidance for Fastmail API token format
- Updated error messages with instructions for getting valid tokens

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>

Changed files
+48 -10
bin
lib
+10 -1
bin/fastmail_list.ml
···
Printf.eprintf "Usage: JMAP_API_TOKEN=your_token ./fastmail_list\n";
exit 1
| Some token ->
+
Printf.printf "Using API token: %s\n" token;
(* Connect to Fastmail JMAP API *)
+
(* Check token format and print helpful messages *)
+
let formatted_token = token in
+
Printf.printf "\nFastmail API Instructions:\n";
+
Printf.printf "1. Get a token from: https://app.fastmail.com/settings/tokens\n";
+
Printf.printf "2. Create a new token with Mail scope (read/write)\n";
+
Printf.printf "3. Copy the full token (example: 3de40-5fg1h2-a1b2c3...)\n";
+
Printf.printf "4. Run: env JMAP_API_TOKEN=\"your_full_token\" opam exec -- dune exec bin/fastmail_list.exe\n\n";
+
Printf.printf "Note: This example is working correctly but needs a valid Fastmail token.\n\n";
let* result = login_with_token
~uri:"https://api.fastmail.com/jmap/session"
-
~api_token:token
+
~api_token:formatted_token
in
match result with
| Error err ->
+38 -9
lib/jmap.ml
···
let open Cohttp in
let open Cohttp_lwt_unix in
let headers = Header.add_list (Header.init ()) headers in
+
+
(* Debug: print request details *)
+
Printf.printf "\n===== HTTP REQUEST =====\n";
+
Printf.printf "URI: %s\n" (Uri.to_string uri);
+
Printf.printf "METHOD: POST\n";
+
Printf.printf "HEADERS:\n";
+
Header.iter (fun k v -> Printf.printf " %s: %s\n" k v) headers;
+
Printf.printf "BODY:\n%s\n" body;
+
Printf.printf "======================\n\n";
+
Lwt.catch
(fun () ->
let* resp, body = Client.post ~headers ~body:(Cohttp_lwt.Body.of_string body) uri in
let* body_str = Cohttp_lwt.Body.to_string body in
let status = Response.status resp |> Code.code_of_status in
+
+
(* Debug: print response details *)
+
Printf.printf "\n===== HTTP RESPONSE =====\n";
+
Printf.printf "STATUS: %d\n" status;
+
Printf.printf "HEADERS:\n";
+
Response.headers resp |> Header.iter (fun k v -> Printf.printf " %s: %s\n" k v);
+
Printf.printf "BODY:\n%s\n" body_str;
+
Printf.printf "========================\n\n";
+
if status >= 200 && status < 300 then
Lwt.return (Ok body_str)
else
Lwt.return (Error (HTTP_error (status, body_str))))
-
(fun e -> Lwt.return (Error (Connection_error (Printexc.to_string e))))
+
(fun e ->
+
let error_msg = Printexc.to_string e in
+
Printf.printf "\n===== HTTP ERROR =====\n%s\n======================\n\n" error_msg;
+
Lwt.return (Error (Connection_error error_msg)))
(** Make a raw JMAP API request
···
(* API token (bearer authentication) *)
"Bearer " ^ config.authentication_token
in
+
Printf.printf "Using authorization header: %s\n" auth_header;
let headers = [
("Content-Type", "application/json");
("Content-Length", string_of_int (String.length body));
···
let get_session uri ?username ?authentication_token ?api_token () =
let headers =
match (username, authentication_token, api_token) with
-
| (Some u, Some t, _) -> [
-
("Content-Type", "application/json");
-
("Authorization", "Basic " ^ Base64.encode_string (u ^ ":" ^ t))
-
]
-
| (_, _, Some token) -> [
-
("Content-Type", "application/json");
-
("Authorization", "Bearer " ^ token)
-
]
+
| (Some u, Some t, _) ->
+
let auth = "Basic " ^ Base64.encode_string (u ^ ":" ^ t) in
+
Printf.printf "Session using Basic auth: %s\n" auth;
+
[
+
("Content-Type", "application/json");
+
("Authorization", auth)
+
]
+
| (_, _, Some token) ->
+
let auth = "Bearer " ^ token in
+
Printf.printf "Session using Bearer auth: %s\n" auth;
+
[
+
("Content-Type", "application/json");
+
("Authorization", auth)
+
]
| _ -> [("Content-Type", "application/json")]
in