nmarus / node-ews

A simple JSON wrapper for the Exchange Web Services (EWS) SOAP API.
MIT License
116 stars 52 forks source link

Invalid or malformed wsdl file #55

Open HaoNLe opened 7 years ago

HaoNLe commented 7 years ago

I am getting:

Error: Invalid or malformed wsdl file: C:\Path\services.wsdl
    at C:\Path\node_modules\node-ews\lib\ews.js:197:20
    at tryToString (fs.js:455:3)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:442:12)

when I try to send an email:

var EWS = require('node-ews')

// Exchange server connection info
var ewsConfig = {
username: 'username',
password: "password",
host: 'ews URL'
};

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; 

// initialize node-ews
var ews = new EWS(ewsConfig);

// define ews api function
var ewsFunction = 'CreateItem'

// define ews api function args
var ewsArgs = {
  "attributes" : {
    "MessageDisposition" : "SendAndSaveCopy"
  },
  "SavedItemFolderId": {
    "DistinguishedFolderId": {
      "attributes": {
        "Id": "sentitems"
      }
    }
  },
  "Items" : {
    "Message" : {
      "ItemClass": "IPM.Note",
      "Subject" : "Test EWS Email",
      "Body" : {
        "attributes": {
          "BodyType" : "Text"
        },
        "$value": "This is a test email"
      },
      "ToRecipients" : {
        "Mailbox" : {
          "EmailAddress" : "email@domain.com"
        }
      },
      "IsRead": "false"
    }
  }
};

// query ews, print resulting JSON to console
ews.run(ewsFunction, ewsArgs)
  .then(result => {
    console.log(JSON.stringify(result));
  })
  .catch(err => {
    console.log(err.stack);
  });

I believe I am connecting correctly since when I provide incorrect credentials I get a NTLM StatusCode 401: Unauthorized. error; this error does not occur when I provide correct credentials and instead I get the first error above: Invalid or malformed wsdl file.

When I go to the EWS URL endpoint in my browser, I can provide my credentials and then get access to a WSDL file thats about 1.6k lines long so I think perhaps there is something incompatible between this node and the WSDL?

If it helps: the Exchange 2010 sp1 server I'm attempting to interact with is hosted on an Azure VM and I can successfully interact with it using the EWS Managed API in an ASP.NET web app.

HaoNLe commented 7 years ago

Update: I took a look at the WSDL file that my program was downloading and this is what it shows:

<html>
    <head>
        <title>Object moved</title>
    </head>
    <body>
        <h2>
            Object moved to
            <a href="%2fews%2fServices.wsdl">here</a>
            .
        </h2>
    </body>
</html>

But when I access https://domain.com/ews/exchange.asmx by browser I'm getting the real WSDL file.

nmarus commented 7 years ago

You are probably getting a 302 redirect which the browser will follow. Follow redirects likely needs to be enabled the code base. What method of authentication are you using? Ntlm or basic?

HaoNLe commented 7 years ago

I'm using NTLM. I deduced this from the fact that when I provide incorrect credentials I get this error: Error: NTLM StatusCode 401: Unauthorized. When I provide correct credentials I get the Invalid or malformed wsdl file that I mentioned in the original post.

nmarus commented 7 years ago

This should be fixed in version 3.2.x now.

HaoNLe commented 7 years ago

Thank you for the quick response! I am still getting the same error however. I updated and then reinstalled node-ews but both times I am still getting the same issue.

HaoNLe commented 7 years ago

For now I'm circumventing this by downloading the xsd and wsdl files directly from my test server and then defining the temp property of ewsConfig as the directory where I placed those files

alacroux commented 7 years ago

@HaoNLe I had the same problem. My mistake was to use the full ews url in the host of the config. I mean, instead of :

const ewsConfig = { username: 'username', password: 'password', host: 'https://hostname/ews/exchange.asmx' };

I used :

const ewsConfig = { username: 'username', password: 'password', host: 'https://hostname' };

And it worked !

HaoNLe commented 6 years ago

I believe I got it to work with something similar! I will update on my progress once I get access to my code again. Thank you Alacroux!

tongerpeng commented 6 years ago

@alacroux thank you so much. I met the same problem and have resolved it with your suggestion!

zachfejes commented 5 years ago

@nmarus I've just run into this issue as well. I'm getting: Error: Invalid or malformed wsdl file: /var/folders/.../services.wsdl

My configuration is as follows:

// process.env.NODE_TLS_REJECT_UNAUTHORIZED has already been set to '0'

const ewsConfig = {
    username: "my_username",
    password: "my_password",
    host: "https://my_host.com",
    auth: "basic"
};

const options = {
    strictSSL: false
};

const ews = new EWS(ewsConfig, options);
const ewsFunction = 'CreateItem';
const ewsArgs = {
    "attributes" : {
      "MessageDisposition" : "SendAndSaveCopy"
    },
    "SavedItemFolderId": {
      "DistinguishedFolderId": {
        "attributes": {
          "Id": "sentitems"
        }
      }
    },
    "Items" : {
      "Message" : {
        "ItemClass": "IPM.Note",
        "Subject" : "Just a test.",
        "Body" : {
          "attributes": {
            "BodyType" : "Text"
          },
          "$value": "This is some content"
        },
        "ToRecipients" : {
          "Mailbox" : {
            "EmailAddress" : "a_valid_email_address@the_host.com"
          }
        },
        "IsRead": "false"
      }
    }
};

ews.run(ewsFunction, ewsArgs).then(result => { ... }).catch(error => { ... });

I'm not sure what I'm doing wrong, but I've tried switching between putting the host url explicitly as the '.../ews/exchange.asmx' and not as suggested above by @alacroux .

Any suggestions?

Alamonall commented 4 years ago

Any solutions? Still? I have the same problem