[SalesForce] Invalid session Id while using node.js and Soap api with heroku

i write a small node.js script for using soap api

var soap = require('soap');
var url = './partner.wsdl';
soap.createClient(url, function(err, client) {
   console.log('Client created');
    client.login({username: 'XXXXXX',password: 'XXXXXX'},function(err,result,raw){

console.log('session id is '+result.result.sessionId);
                    var sheader = {SessionHeader:{sessionId: result.result.sessionId}};
                    client.addSoapHeader(sheader,"","tns","");
                    client.setEndpoint(result.result.serverUrl);
                    console.log('\t\tQuerying attachment...');
 client.query({queryString:'Select id,name,body,contentType From Attachment Where Id =\''+'00P90000009emYeEAI'+'\''},function(err,result2,raw){
                        console.log('\t\tDone.');
                       console.log('query error is');
                        if(err){
                                                        console.log('coming here'+err);

                            console.log (err);
                        }
                        if(result2){
                            console.log(result2.result);

                        }
                    });

});
});

in this in logs i am getting error

Client created
session id is 00D90000000vSiF!AQYAQEXd1m_wzwZYzoPenNKo9Ecn0lGQNi_.yjoBApEMgqKJT21_dYHphSMtSFNdsRxtN5WB2TgPCJtQfHvxyNuYl7GOnsIm
        Querying attachment...
        Done.
query error is
coming hereError: sf:INVALID_SESSION_ID: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session: [object Object]
{ [Error: sf:INVALID_SESSION_ID: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session: [object Object]]
  root: { Envelope: { Body: [Object] } },
  response: 
   { _readableState: 
      { highWaterMark: 16384,
        buffer: [],
        length: 0,
        pipes: null,
        pipesCount: 0,
        flowing: false,
        ended: true,
        endEmitted: true,
        reading: false,
        calledRead: true,
        sync: false,
        needReadable: true,
        emittedReadable: false,
        readableListening: false,
        objectMode: false,
        defaultEncoding: 'utf8',
        ranOut: false,
        awaitDrain: 0,
        readingMore: false,
        decoder: null,
        encoding: null },
     readable: false,
     domain: null,
     _events: 
      { end: [Object],
        readable: [Function],
        close: [Object],
        data: [Function] },
     _maxListeners: 10,
     socket: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: true,
        _opposite: [Object],
        _buffer: [Object],
        _handle: [Object],
        socket: [Object],
        encrypted: [Object],
        authorized: true,
        _controlReleased: true,
        parser: null,
        _httpMessage: [Object],
        ondata: null,
        onend: null,
        npnProtocol: undefined },
     connection: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: true,
        _opposite: [Object],
        _buffer: [Object],
        _handle: [Object],
        socket: [Object],
        encrypted: [Object],
        authorized: true,
        _controlReleased: true,
        parser: null,
        _httpMessage: [Object],
        ondata: null,
        onend: null,
        npnProtocol: undefined },
     httpVersion: '1.1',
     complete: true,
     headers: 
      { date: 'Wed, 11 Jun 2014 12:32:46 GMT',
        'content-type': 'text/xml;charset=UTF-8',
        'content-length': '700',
        connection: 'close' },
     trailers: {},
     _pendings: [],
     _pendingIndex: 0,
     url: '',
     method: null,
     statusCode: 500,
     client: 
      { _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _maxListeners: 10,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: true,
        pair: [Object],
        _pending: null,
        _pendingEncoding: '',
        _pendingCallback: null,
        _doneFlag: false,
        _retryAfterPartial: false,
        _halfRead: false,
        _sslOutCb: null,
        _resumingSession: false,
        _reading: true,
        _destroyed: false,
        _ended: false,
        _finished: true,
        _opposite: [Object],
        _buffer: [Object],
        _handle: [Object],
        socket: [Object],
        encrypted: [Object],
        authorized: true,
        _controlReleased: true,
        parser: null,
        _httpMessage: [Object],
        ondata: null,
        onend: null,
        npnProtocol: undefined },
     _consuming: true,
     _dumped: false,
     httpVersionMajor: 1,
     httpVersionMinor: 1,
     upgrade: false,
     req: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        output: [],
        outputEncodings: [],
        writable: true,
        _last: true,
        chunkedEncoding: false,
        shouldKeepAlive: false,
        useChunkedEncodingByDefault: true,
        sendDate: false,
        _headerSent: true,
        _header: 'POST /services/Soap/u/30.0/00D90000000vSiF HTTP/1.1\r\nUser-Agent: node-soap/0.4.2\r\nAccept: text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8\r\nAccept-Encoding: none\r\nAccept-Charset: utf-8\r\nConnection: close\r\nHost: ap1.salesforce.com:443\r\nContent-Length: 532\r\nContent-Type: text/xml; charset=utf-8\r\nSOAPAction: "urn:partner.soap.sforce.com/query"\r\n\r\n',
        _hasBody: true,
        _trailer: '',
        finished: true,
        _hangupClose: false,
        socket: [Object],
        connection: [Object],
        agent: [Object],
        socketPath: undefined,
        method: 'POST',
        path: '/services/Soap/u/30.0/00D90000000vSiF',
        _headers: [Object],
        _headerNames: [Object],
        parser: null,
        res: [Circular] },
     pipe: [Function],
     addListener: [Function],
     on: [Function],
     pause: [Function],
     resume: [Function],
     read: [Function],
     request: 
      { domain: null,
        _events: [Object],
        _maxListeners: 10,
        readable: true,
        writable: true,
        headers: [Object],
        method: 'POST',
        uri: [Object],
        callback: [Function],
        explicitMethod: true,
        canTunnel: [Object],
        localAddress: undefined,
        pool: {},
        dests: [],
        __isRequestRequest: true,
        _callback: [Function],
        _redirectsFollowed: 0,
        maxRedirects: 10,
        followRedirect: true,
        followAllRedirects: false,
        redirects: [],
        setHost: false,
        originalCookieHeader: undefined,
        _disableCookies: true,
        _jar: undefined,
        port: 443,
        host: 'ap1.salesforce.com',
        clientErrorHandler: [Function],
        _parserErrorHandler: [Function],
        _buildRequest: [Function],
        _handleUnixSocketURI: [Function],
        path: '/services/Soap/u/30.0/00D90000000vSiF',
        httpModule: [Object],
        agentClass: [Object],
        agent: [Object],
        _started: true,
        href: 'https://ap1.salesforce.com/services/Soap/u/30.0/00D90000000vSiF',
        req: [Object],
        ntick: true,
        response: [Circular],
        _destdata: true,
        _ended: true,
        _callbackCalled: true },
     toJSON: [Function: toJSON],
     body: '<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><soapenv:Fault><faultcode>sf:INVALID_SESSION_ID</faultcode><faultstring>INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session</faultstring><detail><sf:UnexpectedErrorFault xsi:type="sf:UnexpectedErrorFault"><sf:exceptionCode>INVALID_SESSION_ID</sf:exceptionCode><sf:exceptionMessage>Invalid Session ID found in SessionHeader: Illegal Session</sf:exceptionMessage></sf:UnexpectedErrorFault></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>' },
  body: '<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><soapenv:Fault><faultcode>sf:INVALID_SESSION_ID</faultcode><faultstring>INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session</faultstring><detail><sf:UnexpectedErrorFault xsi:type="sf:UnexpectedErrorFault"><sf:exceptionCode>INVALID_SESSION_ID</sf:exceptionCode><sf:exceptionMessage>Invalid Session ID found in SessionHeader: Illegal Session</sf:exceptionMessage></sf:UnexpectedErrorFault></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>' }
undefined


 coming hereError: sf:INVALID_SESSION_ID: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session: [object Object]

same issue i am facing on my desktop.

Best Answer

Can you capture the request XML that's generated? looking at the code i'd guess that the sessionHeader is not being created in the correct xml namespace which should be urn:partner.soap.sforce.com. What are the params to addSoapHeader? you should be able to set the namespace on that call.

Related Topic