OAuth authentication for Gmail IMAP in desktop applications

I’m trying to figure out if Google supports OAuth for Gmail IMAP/SMTP in desktop apps. Their docs are confusing me. They say they only support OAuth for web apps, but then they have instructions for installed apps too.

The OAuth spec mentions issues with desktop apps, saying attackers could get the Consumer Secret. But maybe that’s just for Google Data APIs, not IMAP/SMTP?

I thought about using a small web app as a middleman to get the access token, then have the desktop app use that. Is this the only way? If we do OAuth directly in the desktop app, we’d have to include the Consumer Secret, which seems risky.

Has anyone successfully implemented OAuth for Gmail IMAP in a desktop email client? What approach did you use to keep things secure? I’d appreciate any insights on the best way to handle this. Thanks!

I’ve implemented OAuth for Gmail IMAP in a desktop application, and it’s definitely possible.

The key is using the ‘installed application’ flow as outlined in Google’s OAuth 2.0 documentation. This approach avoids storing the client secret in the app.

Here’s how it works: The app opens a browser for the user to log in and grant permissions; then, Google redirects to a local URL that the app monitors. This URL contains an authorization code, which is exchanged for access and refresh tokens.

For IMAP/SMTP, use the access token with the XOAUTH2 mechanism. Securely store the refresh token for obtaining new access tokens when needed, and remember the correct scope: https://mail.google.com/

This method is secure and officially supported for desktop apps, eliminating the need for a middleman web service while keeping sensitive information off the client.

I’ve wrestled with this exact issue in a recent project. Google’s documentation can definitely be confusing on this topic. From my experience, it is possible to implement OAuth for Gmail IMAP/SMTP in desktop applications, but it requires some careful consideration.

We ended up using a hybrid approach. We created a small web service that handles the initial OAuth flow and token retrieval. The desktop app then securely communicates with this service to obtain and refresh tokens as needed. This way, we kept the client secret off the desktop app entirely.

For the actual IMAP/SMTP connection, we used the obtained access token in the XOAUTH2 authentication mechanism. This worked smoothly with Gmail’s servers.

One gotcha to watch out for: make sure you’re using the correct scope for IMAP/SMTP access (https://mail.google.com/). We initially used the wrong scope and spent hours debugging.

While this approach adds some complexity, it provides a good balance of security and user experience. Users still get a native desktop experience, but we avoid exposing sensitive credentials in the client-side code.

yeah, oauth for gmail imap in desktop apps is tricky. i ended up using the ‘installed app’ flow from google’s docs. basically, you open a browser window for the user to log in, then catch the auth code it sends back. Use that to get tokens, then xoauth2 for imap/smtp.

no need for a separate web app. just make sure to use the right scope (https://mail.google.com/) and securely store the refresh token. works pretty well once you figure it out!