Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question

3 Answers 3

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.

share|improve this answer
    
i am following this tutorial enreeco.blogspot.in/2013/11/… .its giving me username of mine email address screen name etc correct but when i try to use sessionId its giving me error.can u please tell how to set this namespace ?? –  user133421 Jun 11 at 14:28
    
thanks thanks thanks issue resolved .for last six hours i was working badly.writing in good form. –  user133421 Jun 11 at 14:57
    
You should accept the answer by @superfell as the correct answer since it solved your problem. –  Jason Lantz Jun 11 at 15:28

I think there are two issues in your question:

  1. Can you use session id to connect to Salesforce SOAP API's from Heroku? The answer is yes. I currently do this without any issues so I don't think a time difference between servers is the cause of your problem. Plus, I would highly doubt there is a time difference as they are all likely sync'ed to a global time server.

  2. The error message looks like you're passing an invalid session value in the sessionId element. What does your console log output show for the sessionId? The error message appears to show [object Object] which is the way Javascript will represent an object as a string. My hunch is that your log output will show [object Object] as the sessionId value. If so, you likely need to call a method of that object to get the actual token string though I'm not sure which method that would be.

share|improve this answer
    
i updated the question .i tried locally but getting same error.above u can see sessionId.which is not valid.Please respond. –  user133421 Jun 11 at 12:40
    
If local also fails, that removes Heroku as a potential source of the issue. What happens if your console log line is only result.result.sessionId without trying to concatenate the value with a string in the console.log statement? –  Jason Lantz Jun 11 at 12:45
    
i use console.log(result.result.sessionId) and output is complete sesssionId '00D90000000vSiF!AQYAQEXd1m_wzwZYzoPenNKo9Ecn0lGQNi_.yjoBApEMgqKJT21_dYHphSMtSFN‌​dsRxtN5WB2TgPCJtQfHvxyNuYl7GOnsIm ' –  user133421 Jun 11 at 12:52
up vote 0 down vote accepted

the issue was as suggested by superfell.i didn't provide xml namespace .if instead of client.addSoapHeader(sheader,"","tns","");

i replace this with client.addSoapHeader(sheader,"","tns","urn:partner.soap.sforce.com"); and every thing works fine.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.