You need to perform every single step that is described there:
https://www.limilabs.com/blog/oauth2-client-credential-flow-office365-exchange-imap-pop3-smtp
Usually people use incorrect ids and/or invalid scope - double check every single value you enter.
Another common mistake is not performing "Grant admin consent" step, it is also important to allow modern authentication, making sure to disable security defaults, and double checking that conditional access policies are not blocking access:
https://www.limilabs.com/blog/office365-enable-imap-pop3-smtp
Double check PowerShell commands as well:
Get-ServicePrincipal
Get-MailboxPermission -Identity "TODO@domain.onmicrosoft.com"
Have in mind, it takes 20-30 minutes for some changes (e.g. enabling IMAP) to take effect.