Office 365 / EWS Authentication using OAuth

后端 未结 3 1224
傲寒
傲寒 2021-01-01 06:17

I\'m trying to log onto Office 365 Exchange Online using OAuth and EWS Managed API.

I am able to use connect to the Office 365 Web API\'s (REST), so I do have a vali

3条回答
  •  余生分开走
    2021-01-01 07:09

    You can use OAuth to connect to EWS (as opposed to REST), however, it's not as smooth. EWS requires the special "Have full access to a user's mailbox" delegated permission in Azure Active Directory, which requires an administrator to register it. This permission also doesn't "travel" outside the organization, so there is no user-consent scenario for EWS. Essentially, the only scenario that this works for is an administrator registering the application for your own organization.

    Check your app registration in Azure and make sure you have that permission assigned.

    Assuming that you have the permission assigned, I've made this work in two ways.

    1. The simplest: Just add the token you get back to the request headers in an Authorization header, like so:

      ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
      string accessToken = GetAccessToken();
      if (!string.IsNullOrEmpty(accessToken))
          service.HttpHeaders.Add("Authorization", "Bearer " + accessToken);
      
    2. More complex: Implement your own credentials class that implements the ICredentials interface. Pass this class into the constructor of the Microsoft.Exchange.WebServices.Data.OAuthCredentials class, and assign the new OAuthCredentials object to the Url property of the ExchangeService object.

提交回复
热议问题