无需用户交互即可获取Office 365 API访问令牌

我想为我的应用程序添加一个function,以便它能够在没有用户交互的情况下向outlook.com添加日历事件。

我见过的所有示例都要求用户登录才能访问office 365 api令牌。 如何在没有用户交互的情况下获得该令牌?

您可以使用客户端凭据来请求令牌而不是OAuth 2.0代码授予流

以下是您的参考请求:

POST https://login.microsoftonline.com//oauth2/token HTTP/1.1 Content-Type: application/x-www-form-urlencoded grant_type=client_credentials &client_id= &client_secret= &resource=https://outlook.office.com 

以下是使用Microsoft.IdentityModel.Clients.ActiveDirectory请求hte令牌的示例:

  public static async Task 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; } 

有关Office 365 REST的更多详细信息,请参阅此处 。