I’ve just tested the automation with a service principal and it’s working perfectly well. In terms of code you would just have to manage the introduction of [encrypted] secret password of your azure application.
I’ve used this code which is simple and effective. I guess it would not a problem for you to implement this. You would have 2 modes : delegated and application
The most complex part according to me is the process : create an azure app in application mode with good scopes, then create azure group and authorize it to use api in the power bi administration, then give rights on workspace to your app, and there it goes. It’s explained here