Thank you for reaching out. While I am not expert on IMAP and Exchange based auth, however I assume following document should help you resolve your issue: Authenticate an IMAP, POP or SMTP connection using OAuth
You can also check following QnA post on which similar question was answered: [https://learn.microsoft.com/en-us/answers/questions/1112032/outlookoffice365-imap-how-to-get-access-token(api)
I hope this answer helps to resolve your issue.
Please "Accept the answer" if the information helped you. This will help us and others in the community as well.