OfficeDev / ews-managed-api

Other
584 stars 319 forks source link

EWS GetAttachments request returns 401 with token from Outlook add-in #232

Open MC-DS opened 5 years ago

MC-DS commented 5 years ago

We're using the code from this sample in the Outlook add-in documentation to download the attachments of a message from an ASP.NET Wep API application; however the call to service.GetAttachments(...) fails with this exception:

Microsoft.Exchange.WebServices.Data.ServiceRequestException
  HResult=0x80131500
  Message=The request failed. The remote server returned an error: (401) Unauthorized.
  Source=Microsoft.Exchange.WebServices
  StackTrace:
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
   at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute()
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   at Microsoft.Exchange.WebServices.Data.ExchangeService.GetAttachments(String[] attachmentIds, Nullable`1 bodyType, IEnumerable`1 additionalProperties)

Inner Exception 1:
WebException: The remote server returned an error: (401) Unauthorized.

This is an example of the request we're sending from the Outlook add-in:

{
    "attachmentToken": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjA2MDBGOUY2NzQ2MjA3MzdFNzM0MDRFMjg3QzQ1QTgxOENCN0NFQjgiLCJ4NXQiOiJCZ0Q1OW5SaUJ6Zm5OQVRpaDhSYWdZeTN6cmciLCJ0eXAiOiJKV1QifQ.eyJvaWQiOiIzY2ExZjdjNS1hMmUwLTRlZTItOWYzNC1iNzljZTlmMWIwZjgiLCJwdWlkIjoiMTAwMzdGRkVBNTJGMTJDMSIsInNtdHAiOiJNYXR0aGV3QGRhcnppbi5jb20iLCJ1cG4iOiJNYXR0aGV3QGRhcnppbi5jb20iLCJjbGllbnRhcHBpZCI6IjAzMTM0YWUxLWQyYzUtNGQzZi05MTI2LTI2MWM2MTZhYzY3MiIsInZlciI6IkV4Y2hhbmdlLkNhbGxiYWNrLlYyIiwiYXBwaWQiOiIwMDAwMDAwMi0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAiLCJkZXBsb3ltZW50aWQiOiJodHRwczovL291dGxvb2sub2ZmaWNlMzY1LmNvbS8iLCJ0aWQiOiI3ZTYyMWZjYy1hN2RkLTRhNDktYTg4Zi01ZmY1ZjlkZWEwNjgiLCJhY3IiOiIxIiwiYXBwaWRhY3IiOiIwIiwic2NwIjoiTWFpbC5SZWFkV3JpdGUgTWFpbC5TZW5kIENhbGVuZGFycy5SZWFkV3JpdGUgQ29udGFjdHMuUmVhZFdyaXRlIiwibmJmIjoxNTcxMzU4MTk3LCJleHAiOjE1NzEzNTkwOTcsImlzcyI6Imh0dHBzOi8vb3V0bG9vay5vZmZpY2UzNjUuY29tLyIsImF1ZCI6Imh0dHBzOi8vb3V0bG9vay5vZmZpY2UzNjUuY29tIn0.qt96NHdROhxMXllF2jGNrEeBWBi7y5y0cTj0gcFLOqHa9rNeQ_UXkBw4IhCwTMiohm0_lQ9Jr0ZBcPFi1QPMnNmebfxps1Zcb7RomWOGz5wxZyHf9ZSKcgiXkBcwJeSMsMiI4o0qNutaJ5MLl3fbTuFBGM0-Y-ePGxa_vZf8-OAqs98p0GKzzIXBxlm8wvhA5kD1JUtFMSUmDTx1NMoAGXfXh-IpwdfrclRZBh5thZk1i7zvfXUmbwbu3JnFxvhtyueOz9ikXVsVgwAITtg_Fsbk8W7-Gk-glZQK0kbFNMq2x3tuvPEBR4RNIYSoPUnY3sdxg7KdClTBYvWFKUG6ew",
    "ewsUrl": "https://outlook.office365.com/EWS/Exchange.asmx",
    "attachments": [
        {
            "id": "AAMkAGYyNGM2NjQ3LTVmYTQtNDRjNy04NmQ5LTlmNmZlOTBkYzY4ZgBGAAAAAACJ3RR+MiahT6iZK/ksGyjdBwDh1+DrdYv3RoCSG267wnQHAAAAAAEMAADh1+DrdYv3RoCSG267wnQHAAHeYc8TAAABEgAQAJ2mUVcGENJAmb9CIyfIZbU=",
            "name": "123.txt",
            "contentType": "text/plain",
            "size": 11886,
            "attachmentType": 0,
            "isInline": false
        }
    ]
}

It should be noted that we're using an unofficial build of the EWS Managed API from the Exchange.WebServices.Managed.Api NuGet package, but the official build from the Microsoft.Exchange.WebServices NuGet package exhibits the same issue.

2015kd commented 2 years ago

I am also facing similar issue, any suggestion please ? FileAttachment.Load() is returning 401 errors.