我想向我的应用程序添加一项功能,以便无需用户交互即可将日历事件添加到 Outlook.com。
我见过的所有示例都要求用户登录才能访问 Office 365 api token 。如何在没有用户交互的情况下获取该 token ?
最佳答案
您可以使用客户端凭据来请求 token ,而不是使用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 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;
}
有关Office 365 REST的更多详细信息,请参阅here .
关于c# - 无需用户交互即可获取 Office 365 API 访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38646611/