Help with (imap-connect ...) using oauth2 (and maybe tls)?

hi, I've successfully connected to imap servers without using oauth2 but am now trying to do so with oauth2 (and maybe tls).

My attempt seems to be timing out (but it also seems to be prompting me for input, as though I had called (read-line ...) or something, and I'm not sure why.

I'm not sure I'm using the function as intended; here's some code I'm trying:

(define (connect-to-gmail username access-token)
  (printf "Connecting to Gmail IMAP server...\n")
  (let ([imap-connection 
         (imap-connect 
          "imap.gmail.com"
          username
          access-token
          "INBOX"
          ;;#:tls? #t
          #:try-tls? #t
          #:xoauth2? #t
          
          )])
    (printf "IMAP connection established.\n")
    imap-connection))

The top printf happens, the bottom doesn't and after a minute or two I get:

tcp-connect: connection failed
  hostname: imap.gmail.com
  port number: 143
  system error: Operation timed out; errno=60

I would appreciate pointers from someone who has an idea what I'm doing wrong.

Cheers, Tim

I succeeded with imap-connect* (as I had previously without oauth2). I guess I should have tried that before posting my question, but I can include the toy code that works in case this might help someone else in a similar situation.

(define (connect-to-gmail username access-token)
  (let ([port-no 993]
        [hostname  "imap.gmail.com"])
    (printf "Connecting to Gmail IMAP server...\n")
    (let-values
        ([(imap-connection messages# nu)
          (let-values ([(in out) (ssl-connect hostname port-no)])
            (imap-connect*
             in
             out
             username
             access-token
             "INBOX"
             ;;#:tls? #t
             #:try-tls? #t
             #:xoauth2? #t
             ))])

      (printf "Connecting to Gmail IMAP server...\n")
      (printf "IMAP connection established.\n")
      imap-connection)))

2 Likes