2

I want to add a feature to my application so that it will be able to add calendar events to outlook.com without user interaction.

All the examples I've seen require user to login to be able to access office 365 api token. How do i get that token without user interaction?

Tone Škoda
  • 1,463
  • 16
  • 20

1 Answers1

2

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.

Fei Xue
  • 14,369
  • 1
  • 19
  • 27
  • If I understand correctly I need to pay to get tenantId? – Tone Škoda Aug 06 '16 at 10:40
  • Yes. You can login the classic Azure portal and you can get the tenantId from the URL of browser like this https://manage.windowsazure.com/rekenoutlook.onmicrosoft.com#Workspaces/ActiveDirectoryExtension/Directory/{tenantId}/directoryQuickStart. – Fei Xue Aug 08 '16 at 01:10
  • @FeiXue-MSFT Hi, can I use this access token to read the email from my office 365 account? – ashish jayara Apr 16 '20 at 14:07
  • Microsoft.IdentityModel.Clients.ActiveDirectory is marked obsolete now. This solution is out of date. – Shiv Nov 29 '22 at 03:15