What's the best approach for OAuth2 authentication in automated systems without browsers?

I’m working with automated systems that need to authenticate with various APIs using OAuth2, but I’m running into issues since these systems don’t have browsers or user interaction. The standard OAuth2 flow expects a user to click through authorization pages, which obviously doesn’t work for headless applications.

I’ve been looking into different approaches like device flow or workload identity federation, but I’m not sure which one is the most practical solution. Has anyone dealt with this problem before? What method did you end up using for your headless applications to get proper API access without requiring manual authorization steps?

For headless systems, client credentials flow works great - it’s built for machine-to-machine auth. No browser stuff needed since your app talks directly to the auth server with client ID and secret. You swap those credentials for an access token, zero user interaction. Main thing is you need to register as a confidential client and the API provider has to support it. Google Cloud, Microsoft Graph, AWS all do. Just implement token refresh properly and store your credentials securely. We’ve been using it for automated data pipelines and it’s rock solid.

service accounts are the way to do it if the provider allows. no user auth mess, just generate keys and you’re good. i’ve used em with google apis and slack, way easier than getting into refresh tokens. don’t forget to rotate those keys tho!

Device flow works exceptionally well for scenarios where APIs lack support for client credentials. I implemented it for a monitoring system requiring access to multiple SaaS APIs. The process involves a one-time setup where a code is displayed on any internet-enabled device. The user visits the auth URL, inputs the code, and your headless application receives the refresh token. This enables your automated system to operate indefinitely by using the refresh token to obtain new access tokens without user interaction. It’s crucial to store that refresh token securely and ensure proper token refresh logic. I’ve found this approach particularly effective with APIs such as Microsoft Graph and Spotify, which offer robust device flow support.