function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CovenantCovenant 

Trouble using 'session_id' returned by login Response.

Hello,

I am having some difficulty using the 'session_id' returned by the login Response using Python 2.3.  Prior to this version of Python, xmlrpclib would always fail because it wasn't able to deal with Boolean types.  I am quite new to XMLRPC, so please forgive me if the question is ignorant.

Here's the, quite simple, code I am using:

----------

import xmlrpclib

loginServer = xmlrpclib.Server("https://www.salesforce.com/servlet/servlet.Api")
loginParams = {'version':'2.0',
               'username':'covenantr@yahoo.com',
               'password':'luvPilar',
               'secure':True
               }

loginResponse = loginServer.sfdc.login(loginParams)

print loginResponse

transactionServer = xmlrpclib.Server( loginResponse['server_url'] )

logoutParams = {'version':'2.0',
                'session_id':loginResponse['session_id']
                }

logoutResponse = transactionServer.sfdc.logout(logoutParams)

----------

Here's what I get as output:

--------

$ python scripts/sforceXMLRPC.py
{'userID': '00530000000c4BV', 'server_url': 'https://na1-api.salesforce.com/serv
let/servlet.Api', 'session_id': 'rZVyAYxJHzioUs1Tx5x9CeWp5QeKVIIPcwDAUguWiqV9vYt
04QqF5QemGXwg.IDWkWByTFHyy2rvJ4CmaYRpUfbtzKggVlhL'}
Traceback (most recent call last):
  File "scripts/sforceXMLRPC.py", line 43, in ?
    logoutResponse = transactionServer.sfdc.logout(logoutParams)
  File "c:\Python23\lib\xmlrpclib.py", line 1029, in __call__
    return self.__send(self.__name, args)
  File "c:\Python23\lib\xmlrpclib.py", line 1316, in __request
    verbose=self.__verbose
  File "c:\Python23\lib\xmlrpclib.py", line 1080, in request
    return self._parse_response(h.getfile(), sock)
  File "c:\Python23\lib\xmlrpclib.py", line 1219, in _parse_response
    return u.close()
  File "c:\Python23\lib\xmlrpclib.py", line 742, in close
    raise Fault(**self._stack[0])
xmlrpclib.Fault: <Fault 1112: 'session ID missing or invalid'>

-----------

As you can see, the loginResponse dictionary looks correct.  I "think" I'm doing the correct thing by creating a new server instance for the remaining transactions, while grabbing the 'session_id' and other parameters from the login Response.

Any hints, ideas, or help into why this is failing would be great!!

Thanks and Best Regards,

- Covenant

CovenantCovenant

Ok.... I am still hitting the same problem, but I have more info....  Below is a complete debug listing of the output of the script I'm running:

---------------------------

43> python2.3 ./bin/Python/sforceXMLRPC.py
send: 'POST /servlet/servlet.Api HTTP/1.0\r\nHost: www.sforce.com\r\nUser-Agent xmlrpclib.py/1.0.1 (by www.pythonware.com)\r\nContent-Type text/xml\r\nContent-Length: 499\r\n\r\n'
send: "\n\nsfdc.login\n\n\n\n\nusername\ncovenantr@yahoo.com\n\n\npassword\nslacker69\n\n\nversion\n2.0\n\n\nsecure\n1\n\n\n\n\n\n"
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Wed, 06 Aug 2003 18:36:18 GMT
header: Server: Apache
header: Set-Cookie: sfdcweb=65.213.42.132.194511060194978591; path=/; max-age=31536000
header: Set-Cookie: sid=vwxw8sPENpaDeLJgAQHt32W.W3A.eUH_B.hfoF8sylY9DjH7G27.Am2.dNnSI8zQWR1EFY6Muc1Dg9jOlW0BT.btzKggVlhL; path=/
header: Connection: close
header: Content-Type: text/xml; charset=UTF-8
body: 'server_urlhttps://na1-api.salesforce.com/servlet/servlet.ApiuserID00530000000c4BVsession_idvwxw8sPENpaDeLJgAQHt32W.W3A.eUH_B.hfoF8sylY9DjH7G27.Am2.dNnSI8zQWR1EFY6Muc1Dg9jOlW0BT.btzKggVlhL'
{'userID': '00530000000c4BV', 'server_url': 'https://na1-api.salesforce.com/servlet/servlet.Api', 'session_id': 'vwxw8sPENpaDeLJgAQHt32W.W3A.eUH_B.hfoF8sylY9DjH7G27.Am2.dNnSI8zQWR1EFY6Muc1Dg9jOlW0BT.btzKggVlhL'}
send: 'POST /servlet/servlet.Api HTTP/1.0\r\nHost: na1-api.salesforce.com\r\nUser-Agent: xmlrpclib.py/1.0.1 (by
www.pythonware.com)\r\nContent-Type text/xml\r\nContent-Length: 522\r\n\r\n'
send: "\n\nsfdc.get_server_timestamp\n\n
\n\n\nversion\n2.0\n\n\nuserID\n00530000000c4BV\n\n\nsession_id\nvwxw8sPENpaDeLJgAQHt32W.W3A.eUH_B.hfoF8sylY9DjH7G27.Am2.dNnSI8zQWR1EFY6Muc1Dg9jOlW0BT.btzKggVlhL\n\n\n\n\n\n"
reply: 'HTTP/1.0 200 OK\r\n'
header: Server: Resin/2.1.9
header: Content-Type: text/xml; charset=UTF-8
header: Date: Wed, 06 Aug 2003 18:36:19 GMT
body: 'faultStringsession ID missing or invalidfaultCode1112'
Traceback (most recent call last):
  File "./bin/Python/sforceXMLRPC.py", line 56, in ?
    transactionServer.sfdc.get_server_timestamp( timestampParams )
  File "/usr/lib/python2.3/xmlrpclib.py", line 1029, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib/python2.3/xmlrpclib.py", line 1316, in __request
    verbose=self.__verbose
  File "/usr/lib/python2.3/xmlrpclib.py", line 1080, in request
    return self._parse_response(h.getfile(), sock)
  File "/usr/lib/python2.3/xmlrpclib.py", line 1219, in _parse_response
    return u.close()
  File "/usr/lib/python2.3/xmlrpclib.py", line 742, in close
    raise Fault(**self._stack[0])
xmlrpclib.Fault:
----------------------

Am I doing something incorrectly?  I thought all I had to do was grab the 'server_url', 'session_id' and, perhaps, the 'userID' from the login Response and use this information to format any further API requests.  Could it be a type issue?  Python, unless otherwise specified, sends these requests with the "string" type.  Could that be an issue?  Also, is it a problem that my HTML headers do not contain the "Cookies" for sid and sfdcweb?

Any help would be greatly appreciated!

Best Regards,

- Covenant (Brendan Rankin @ Magma DA)

CovenantCovenant

Solved my problem.  It seems all HTML headers must contain the Cookie info sent along with the XMLRPC login Response.

This leads to two questions:

1.  Why require the HTML cookies at all?  It seems like just validating the XMLRPC packet should be good enough, right?

2.  Why does missing an HTML cookie return an XMLRPC *fault*?  Shouldn't it return an HTML protocol error??

I'll post this as another topic, when I get the chance.

- Covenant

DevAngelDevAngel

Hi Covenant,

Glad to hear you found the problem.  As to the questions:

1.  This is an artifact from the evolution of our XML-RPC implementation.  It's not onerous, and our resources now and in the future are committed to SOAP.  XML-RPC will remain usable, be will be depracted in 2 or 3 years.

2.  Good question.  Philosophically, I don't like having anything related to authentication in the HTTP Header.  If you think in terms of a federated service that may pass requests from one service to another you begin to see how this might cause problems.  As to the error being a fault, the service sees the message as missing the session_id.  The HTTP protocol is not necessarily in error and the authentication is not happening at the protocol level.  I'm guessing that if we sent back a HTTP protocol error like 406.1 someone would ask why it is an HTTP error and not a fault.

Would you mind posting the login code for python?

Message Edited by DevAngel on 08-07-2003 03:11 PM

CovenantCovenant
I understand, I was just curious. Dunno if I like SOAP all that well either.... I might try it out using ZSI or some other language. I like using Python because its native "dictionary" data structure maps perfectly to the nested structs and arrays of XMLRPC or SOAP.

Here's the login code I used for python (it's really simple):
-------------------------
#!/usr/bin/env python2.3
#
# Must be python2.3. Prior versions do not support native Boolean types!
#
# Imports. Using pyxmlrpclib from Sourcforge because it's *much* faster!
#
import pyxmlrpclib

#
# Create a login ServerProxy.
#
loginServer = xmlrpclib.Server("https://www.sforce.com/servlet/servlet.Api",verbose=1)
#
# Create the parameters to pass to the login server.
#
loginParams = {'version':'2.0',
'username':'',
'password':'',
'secure': True
}
#
# Now send the request Login call.
# Capture the Response.
#
loginResponse = loginServer.sfdc.login(loginParams)
------------------------

For anyone who's interested, I should have a reasonably complete set of typical calls finished in the next few weeks. I don't have an external website to post it to, but if you're willing to store the files, I'll pass them along.

Regards,

- Covenant
DevAngelDevAngel

Thanks for the posting Covenant 

julioallenjulioallen

Hello,

I had a similar problem, and worked out some modifications to the xmlrpclib library which I thought I'd post here to (hopefully) save others from having to figure it out - also, I'm new to all of this (Python, xmlrpc and sforce) so if I've gone astray I'd appreciate some pointers...

(Before doing anything, make of copy of the original library, in case it all goes pear-shaped.)

1. In the send_content function of the Transport class, add a parameter: session_id=None

2. In the same function, add the following lines at the top of the function:

        if session_id:
            connection.putheader("Cookie", "sid=" + session_id)

3. In the request function of the Transport class, add a parameter: session_id=None

4. In the same function amend the call to self.send_content to include the sessionID variable

5. In the __request function of the ServerProxy class, add the following lines at the top of the function:

        try:
            sessionID = params[0]['session_id']
        except:
            sessionID = None

6. In the same function amend the call to self.__transport.request to include the sessionID variable

And that should be it - all you do is grab the session ID from the parameters variable (if it exists) and add it to the HTTP header...

Also, you should probably call the ServerProxy method, rather than the Server method, as the latter exists for backward compatability only.

Cheers,

Julio

 

 

Test.ax41Test.ax41

> For anyone who's interested, I should have a reasonably complete set of typical calls

> finished in the next few weeks. I don't have an external website to post it to, but if you're

> willing to store the files, I'll pass them along.

> Regards,

> - Covenant

Hi Covenant,

I am interested in hearing more about your python experience.  How did it go? What module did you use to handle the WSDL file? Are the sources now availables?  Please contact me via "arsanalytica at yahoo"

Thank You!

Dan

 

adamrice32adamrice32
Hi Convenant,

I am going to be doing some sforce development with Python in the coming weeks, and I would be very interested to see what you've come up with in terms of basic calls. You can email me at adamrice32@yahoo.com.

Thanks in advance!

Best,
Adam Rice