PC SOFT

PROFESSIONAL NEWSGROUPS
WINDEVWEBDEV and WINDEV Mobile

Home → WINDEV 2024 → Settings for Outlook Oauth2
Settings for Outlook Oauth2
Started by simon, Sep., 26 2024 2:32 PM - 3 replies
Posted on September, 26 2024 - 2:32 PM
Hi All

Does anyone have working setting for recovinging the auth token from Microsoft so that I can run SMTP to outlook.com accounts. Since Microsoft have applied Oauth2 to outlook.com, I cannot now use emails. Fortunatley the app is still in development, but this is a must.

The code I am currently using is:

OAuthOutlookCnt is OAuth2Parameters
OAuthOutlookCnt.ClientID = "MyClientID"
OAuthOutlookCnt.ClientSecret = "MyClientSecret"
OAuthOutlookCnt.AuthURL="https://login.microsoftonline.com/MytenantID/oauth2/v2.0/authorize"
OAuthOutlookCnt.TokenURL="https://login.microsoftonline.com/MytenantID/oauth2/v2.0/token"
OAuthOutlookCnt.Scope="https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/SMTP.Send"
OAuthOutlookCnt.RedirectionURL = "http://localhost:1998" 
OAuthOutlookCnt.ResponseType = oauth2ResponseTypeCode

MyToken = AuthIdentify(OAuthOutlookCnt)
IF MyToken.Valid = False THEN
STC_Failed.Visible = True
ELSE
STC_Success.Visible = True
END

(MyClientID, MyClientSecret and MytenantID are substitued for the actual codes)

I have tried everything but MyToken.Valid always returns False.
I have it working fine with Gmail, but cannot get this to return a token.
Any suggestions would be greatly appreciated before I finish pulling the last of my hair out!

Thanks
Simon
Posted on September, 27 2024 - 8:00 PM
Hi Simon,

It sounds like you're close but just need a few adjustments to get the OAuth2 flow working for Microsoft's new authentication requirements for Outlook.com. Here are some things to check and troubleshoot based on your current setup:

1) Check Your Redirect URI:
Make sure that http://localhost:1998 is registered as a valid Redirect URI in your Azure app registration. Microsoft is strict about matching the redirect URI exactly, so if there's any mismatch, it will cause authentication failures.
Also, consider using https://localhost:1998 instead of HTTP, as some OAuth2 flows prefer HTTPS for security reasons.
2) Token Scopes:
Your scopes look good, but ensure that the permissions are correctly set in the Azure App Registration for the API permissions. You should have:
IMAP.AccessAsUser.All
SMTP.Send
Go to Azure Portal > App Registrations > API permissions and verify these scopes are granted and admin consent is applied (if necessary).
3) Grant Admin Consent:
If your app requires admin consent for the scopes you've requested, make sure you’ve granted it in Azure AD. Without it, the token request will fail silently.
You can do this from API permissions in the Azure portal by clicking Grant admin consent.
4) Ensure Correct Token Flow:
You're using Authorization Code flow (oauth2ResponseTypeCode), which is correct for this scenario, but ensure your app is also set up to handle the authorization code and token exchange properly.
After you get the authorization code from the auth URL, you'll need to exchange it for an access token at the Token URL.
5) Inspect Error Response:
Try logging the actual error response you’re receiving when MyToken.Valid = False. This will give you more insight into whether it’s an issue with authentication, permissions, or token exchange.
6) Verify Tenant ID:
Double-check your tenant ID. If you're using a personal Microsoft account (like for Outlook.com), the tenant ID might be common or consumers instead of your organizational tenant ID.
By ensuring that all the necessary configurations are in place (especially around permissions and redirect URI), you should be able to get the token successfully.

If you’re still stuck, Microsoft’s MSAL library is a good tool to use for managing OAuth2 flows. It’s worth exploring if you want to simplify the token retrieval process. Best of luck with your <a href="https://medhacloud.com/microsoft-365-migration-services/">Microsoft 365 migration</a>


Hope this helps, and good luck!
Posted on October, 04 2024 - 3:24 PM
Hi Justa

Sorry for not getting back to you earlier and thanks for your detailed response. I have now manged to get the Auth code, but still cannot start a smtp session. The code returned is in Json rather that an AuthToken format so that could be the problem.
Sorry to be a pain, but do you have working code that can be used to get the auth token and then start an smtp session with outlook.com as I am obviously missing something? I seem to be almost there but still refusing connection.
Just to be clear this is using Outlook.com and not office365.

Thanks for your help
Simon
Posted on January, 14 2025 - 7:43 PM
Simon,

Are you still working on this?
I can post an example for you.

Jim