无需用户交互即可获取Office 365 API访问令牌 [英] Get Office 365 API access token without user interaction

查看:265
本文介绍了无需用户交互即可获取Office 365 API访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中添加功能,以便它无需用户交互即可将日历事件添加到Outlook.com.

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.

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

All the examples I've seen require user to login to be able to access office 365 api token. How do i get that token without user interaction?

推荐答案

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

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

这是使用Microsoft.IdentityModel.Clients.ActiveDirectory请求HTE令牌的示例:

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;
    }

有关Office 365 REST的更多详细信息,请参考

More detail about Office 365 REST, please refer here.

这篇关于无需用户交互即可获取Office 365 API访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆