this project demos how to retrieve an AAD S2S token for a SharePointOnline (SPO) tenant for a given AAD client application.
The AAD client application in the project is a first party application, but the code should works for any client application.
This project use client certificate (p12) file to authenticate with AAD and you can find how to create a self-signed client certificate file from this link