V14 M365 Emails giving error while Authorize API Access

I am on the v14 of ERPNext and trying to connect to M365 for email accounts.

As far as I understand, I have created an app registration in Active Directory with the following permissions

image

Then I have configured the Connected App with the scopes

Clicking on "Connect to Microsoft " on the connected apps page after entering the OpenID configs has worked and there is a Token added in the Token Cache as well.

Next an email account is added. The email account is for the same user that exists in the AD and has logged in. The connected app has been chosen. As well as the “Always use this email address as sender address”

But when I click on the “Authorize API Access” button I get the following error message. Any pointers on what is going wrong?

Blockquote
Traceback (most recent call last):
File “apps/frappe/frappe/app.py”, line 66, in application
response = frappe.api.handle()
File “apps/frappe/frappe/api.py”, line 54, in handle
return frappe.handler.handle()
File “apps/frappe/frappe/handler.py”, line 45, in handle
data = execute_cmd(cmd)
File “apps/frappe/frappe/handler.py”, line 83, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “apps/frappe/frappe/init.py”, line 1607, in call
return fn(*args, **newargs)
File “apps/frappe/frappe/integrations/doctype/connected_app/connected_app.py”, line 149, in callback
frappe.throw(_(“Invalid state.”))
File “apps/frappe/frappe/init.py”, line 525, in throw
msgprint(
File “apps/frappe/frappe/init.py”, line 493, in msgprint
_raise_exception()
File “apps/frappe/frappe/init.py”, line 442, in _raise_exception
raise raise_exception(msg)
frappe.exceptions.ValidationError: Invalid state.

Blockquote