c# - 无需用户交互即可获取 Office 365 API 访问 token

标签 c# office365 office365api

我想向我的应用程序添加一项功能,以便无需用户交互即可将日历事件添加到 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/

相关文章:

c# - xamarin Android.Util.AndroidRuntimeException : Activity{x} did not call through to super. onStart()

Excel 无法加载基本的自定义函数加载项

outlook - 在不同时区使用 Outlook 365 API 设置全天日历事件

VBA 从 OneDrive 下载文件

c# - Office 365 统一 api(预览版)组文件仅在某些租户中返回 500 内部服务器错误

c# - 在 WebCamTexture 中检测人物的最佳方法

C#大纲编号

javascript - 使用 Office JS Word API 从对话框访问 Word 文档上下文

c# - 减少 C# 中的位图位大小

c# - 如何从exchange获取所有通讯组列表