获取Office 365 API访问令牌,无需用户交互
本文关键字:用户 交互 访问令牌 Office API 获取 | 更新日期: 2023-09-27 17:59:04
我想为我的应用程序添加一个功能,这样它就可以在没有用户交互的情况下将日历事件添加到outlook.com。
我看到的所有例子都要求用户登录才能访问office 365 api令牌。如何在没有用户交互的情况下获得该令牌?
您可以使用客户端凭据来请求令牌,而不是OAuth 2.0代码授予流。
以下是供您参考的请求:
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
下面是使用Microsoft.IdentityModel.Clients.ActiveDirectory
请求hte令牌的示例:
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;
}
有关Office 365 REST的更多详细信息,请参阅此处。