I would watch the following
API Authentication with OAuth using Azure AD
Technically speaking you should separate these concerns. First I would try the OAuth flow in an API testing tool like Postman or Bruno. Then I would try to implement it in Mendix. I don't think it's a Mendix specific problem.