Python Simple Salesforce

There is a way to log in with simple-salesforce with only a username and password. No security token required:

from simple_salesforce import Salesforce, SalesforceLogin

session_id, instance = SalesforceLogin(username='<user>', password='<pass>')
sf = Salesforce(instance=instance, session_id=session_id)
# Logged in! Now perform API actions, SOQL queries, etc.
sf.query_all('<soql>')

Explanation

All examples using simple-salesforce begin with a call to the Salesforce constructor to log in. This constructor accepts either an existing session ID, or authentication credentials to log in and make a new session. When logging in, it calls the lower-level SalesforceLogin function to do the real work, but interestingly SalesforceLogin does not enforce the same constraints on its arguments—it issues the correct SOAP call to log in with just a username and password, without requiring a token or organization ID.

Using this trick, we call SalesforceLogin directly, obtain the new session ID, then pass it directly into the Salesforce constructor. From that point on, we are able to make authenticated API requests.

Note

The version of simple-salesforce on PyPI (i.e. pip install simple-salesforce) is very outdated with the simple-salesforce GitHub repository. The latest version supports additional login parameters like domain for login with custom domains. To get the latest version, use

pip install --upgrade https://github.com/simple-salesforce/simple-salesforce/archive/master.zip

(Pip-installing from zip is faster than using git+ssh:// or git+https://, as noted in this answer.)


I wrote most of simple-salesforce (although not the organizationId part, as I don't have an IP-whitelisted account to test against)

The standard/vanilla/regular/99% of users should use version is the simple username, password, security_token method.

So something like this

from simple_salesforce import Salesforce
sf = Salesforce(username='[email protected]', password='nickspassword', security_token='tokenemailedtonick')

By far the most confusing part is the security_token part (and was the part I got snagged with.) It turns out the Security Token is emailed to you after a successful password reset. So if you go into your salesforce account and reset your password, I believe you'll end up with an email with the subject salesforce.com security token confirmation which will contain a Security Token in the email. That's your security_token.

To be honest, the security_token kwarg is more a convenience than anything. In the normal email/password/token flow that most users rely on what is actually being sent is email as the login and {password}{security_token} as the password. I believe you could concat that yourself and just pass in a email and password kwarg if you want, but I figured forcing people to concat the password and token themselves would get go against the simple part of simple-salesforce


Edit

How will resetting my password show me what the token is?

It just will. If user has ever before requested the security token (which is sent to you via email - so you need to have access to the email address associated with your user) - every subsequent password reset will result with new token being generated and emailed to you. On top of that, once you're logged in to the system (to the web version, not via API) you will have an option to reset your token (and again, this will send you an email).

It's like you haven't read or tried anything we have written!

Looking for an answer drawing from credible and/or official sources.

https://help.salesforce.com/htviewhelpdoc?id=user_security_token.htm

https://help.salesforce.com/HTViewSolution?id=000004502

https://help.salesforce.com/HTViewSolution?id=000003783

And from the library's documentation:

https://github.com/neworganizing/simple-salesforce

To login using IP-whitelist Organization ID method, simply use your Salesforce username, password and organizationId

This. If your IP address is whitelisted - you don't need the token. If it isn't - you NEED to generate the token. Period.


Original answer

I'm not familiar with that Python library but... Go to Salesforce -> Setup -> My personal infromation and check login history. if it contains stuff like "failed: security token required" then you're screwed and you will have to use the security token.

I'm not aware of any bypass that uses org id (I've connected via API from PHP, Java, C#... so I'd be very surprised if that Python library had some magical way to bypass it. You probably are used to passing a session id that assumes you're already authenticated and have a valid session.

Another option would be to check your IP and add it to trusted IP ranges (it's an option in the setup). It's useful when for example whole office has same static IP; less useful if you're working from home.

If that's also a no-go - you might want to look for libraries that use OAuth2 instead of regular SOAP API to authenticate.


Although this is kinda late, somebody searching for this very same issue may be helped as to what I did.

I struggled by adding the company ID as well, but the problem here is, unless you're a self-service user, the company ID can be blank.

sf = Salesforce(password='password', username='email', organizationId='')

As other users mentioned, make sure you're using IP-White listing or it will not work.