Get Office 365 API access token without user interaction

前端 未结 1 1760
心在旅途
心在旅途 2020-12-17 06:47

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

相关标签:
1条回答
  • 2020-12-17 07:20

    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.

    0 讨论(0)
提交回复
热议问题