You can use the Client Credential to request the token instead of OAuth 2.0 Code Grant flow.
Here is the request for your reference:
POST https://login.microsoftonline.com/<tenantId>/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&client_id=<clientId>
&client_secret=<clientSecret>
&resource=https://outlook.office.com
And here is the sample using the Microsoft.IdentityModel.Clients.ActiveDirectory
to request hte token:
public static async Task<string> GetTokenAsync(string resource, string clientId, string secrect)
{
string authority = "https://login.microsoftonline.com/{yourTenantName}";
AuthenticationContext authContext = new AuthenticationContext(authority);
ClientCredential clientCredential = new ClientCredential(clientId, secrect);
AuthenticationResult authResult=await authContext.AcquireTokenAsync(resource, clientCredential);
return authResult.AccessToken;
}
More detail about Office 365 REST, please refer here.