How do I access the Salesforce API when single-sign on is enabled?

Question:

I’m attempting to make SOQL queries to the Salesforce API using the Python salesforce_api and simple-salesforce modules. I had been making these requests with a client object:

client = Salesforce(username='MY_USERNAME',
                    password='MY_PASSWORD',
                    security_token='MY_SALESFORCE_SECURITY_TOKEN')
a = client.query("SELECT something FROM some_object_table WHERE some_condition")

However, my company recently restricted Salesforce sign-in through SSO only (you used to be able to login directly to Salesforce without SSO), and the funciton is throwing either:

simple_salesforce.exceptions.SalesforceAuthenticationFailed: INVALID_SSO_GATEWAY_URL: the single sign on gateway url for the org is invalid

Or:

salesforce_api.exceptions.AuthenticationMissingTokenError: Missing or invalid security-token provided.

depending on which module I use. I suspect this is because of the SSO implementation.

I’ve seen the docs about creating a new app through Okta, but I need to authenticate and access the API of an existing app. What is the best way to access this API with Okta IdP enabled? It there a way to have a get request to Okta return an access token for Salesforce?

Asked By: Nathan Cung

||

Answers:

Uh. It’s doable but it’s an art. I’ll try to write it up but you should have a look at "Identity and Access Management" Salesforce certification, study guides etc. Try also asking at salesforce.stackexchange.com, might get better answers and Okta specialists.

I don’t know if there’s pure server-side access to Okta where you’d provide OAuth2 client, secret, username and password and it’d be silently passed to login.

If your app is a proper web application that needs human to operate – you can still make it work with SSO. You’d have to read about OAuth2 in general (you saw it on the web, all the "login with Google/Facebook/LinkedIn/Twitter/…" buttons) and then implement something like this or this. Human starts in your app, gets redirected to SF to enter username and password (you don’t see password and you don’t care whether he encountered normal SF login page or some SSO), on success he/she is redirected back and you receive info that’ll let you obtain session id (sometimes called access token). Once you have access token you can make queries etc, it’s just a matter of passing it as HTPP Authorization Bearer header (simple-salesforce docs mention session id at top of the examples).

Look, I know what I’ve written doesn’t make much sense. Download Data Loader and try to use it. You might have to make it use custom domain on login but there is a way for it to still work, even though you have SSO enforced. Your goal would be to build similar app to how Data Loader does it. This might help a bit: https://stackoverflow.com/a/61820476/313628

If you need a true backend integration without human involved… tricky. That might be a management problem though. They should not enforce SSO on everybody. When Okta’s down you’re locked out of the org, no way to disable SSO. You should have a backup plan, some service account(s) that don’t have SSO enforced. They might have crazy password requirements, maybe login only from office IP address, whatever. It’s not a good idea to enforce SSO on everybody.

https://help.salesforce.com/articleView?id=sso_tips.htm

We recommend that you don’t enable SSO for Salesforce admins. If your
Salesforce admins are SSO users and your SSO server has an outage,
they have no way to log in to Salesforce. Make sure that Salesforce
admins can log in to Salesforce so that they can disable SSO if
problems occur.

(If you have a web app and it’s embedded as Canvas in SF – there’s another clean way to have the session id passed to you. Again – this works only if you have a human rather than backend integration)

Answered By: eyescream

If you check the profiles in SFDC and uncheck the box that requires SSO.

"is single sign-on Enabled [] Delegate username and password authentication to a corporate database instead of the salesforce.com user database. "

Answered By: Dallen Bennett
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.