Get Office 365 API access token without user interaction

我的未来我决定 提交于 2019-11-28 02:06:41

问题


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.

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?


回答1:


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

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

More detail about Office 365 REST, please refer here.



来源:https://stackoverflow.com/questions/38646611/get-office-365-api-access-token-without-user-interaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!