working with EWS webservice

Abdulqader Al-Jupeh 0 Reputation points
2024-07-26T11:47:26.21+00:00

the server is working, and I copied the code from MSDN

https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth#add-an-authentication-token-to-ews-requests

But the response is "The request failed. The remote server returned an error: (403) Forbidden."

Exchange Server
Exchange Server
A family of Microsoft client/server messaging and collaboration software.
1,238 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jake Zhang-MSFT 4,350 Reputation points Microsoft Vendor
    2024-07-29T08:44:17.79+00:00

    Hi @Abdulqader Al-Jupeh,

    Welcome to the Microsoft Q&A platform!

    Based on your description, a 403 Disabled Access Error usually indicates that your request was denied due to permissions or authentication issues. Here are some possible solutions:

    1. Make sure the OAuth credentials you are using have sufficient permissions to access the EWS API. You may need to assign appropriate permissions to your app in the Azure portal (e.g. EWS.AccessAsUser.All).
    2. Make sure the access token you obtain is valid, not expired, and is correctly added to the Authorization header in the request.
    3. The scope of the request must be consistent with the scope set when your application is registered in Azure Active Directory.
    4. Make sure your EWS request URL is correct and conforms to the required format. For example, if you are using Office 365, the URL should be similar to https://outlook.office365.com/EWS/Exchange.asmx.

    Here is a concise sample code to refer to how to add an OAuth token:

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
    service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
    service.Credentials = new OAuthCredentials("YOUR_ACCESS_TOKEN");
    
    try
    {
        // Example request to test the connection
        Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
        Console.WriteLine("Inbox folder display name: " + inbox.DisplayName);
    }
    catch (ServiceRequestException ex)
    {
        Console.WriteLine("Service request failed: " + ex.Message);
    }
    catch (UnauthorizedAccessException ex)
    {
        Console.WriteLine("Unauthorized: " + ex.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("An error occurred: " + ex.Message);
    }
    

    Please feel free to contact me if you have any queries.

    Best,

    Jake Zhang


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.