Get Office 365 API access token without user interaction

心不动则不痛 提交于 2019-11-29 08:45:09

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.

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