- Published on
Logging into Salesforce Programmatically
- Authors
- Name
- Bryan Thode
While impersonating a user on the same organization works, I needed to frequently switch users on the same instance but different organizations for test purposes.
Bash Approach
curl -d un=$username -d pw=$password -X POST $hostname -si | rg -oP 'Location: \K.*'
Either define the expected shell variables above, or adjust the code to accept them as arguments. Also note that I'm using ripgrep, but vanilla grep should also work. While the username and password should be obvious, the hostname should be the instance your organization resides on, like https://na1.salesforce.com.
The url returned can be used to log into your Salesforce session without being prompted for credentials.
org-babel Approach
For those of the Emacs persuasion, we can directly open the browser to the provided url.
#+NAME: credentialedLogin
#+begin_src sh :var hostname=getLoginURL username=getSFDCUserName password=getSFDCPassword :result silent
curl -d un=$username -d pw=$password -X POST $hostname -si | rg -oP 'Location: \K.*'
#+end_src
#+begin_src emacs-lisp :var host=credentialedLogin token=getToken :result silent
(browse-url host)
#+end_src
Above I'm making use of helper functions to provide the variables already defined and pass them to org-babel.
#+NAME: getLoginURL
#+begin_src emacs-lisp :result silent
sfdc.loginurl
#+end_src
For getLoginURL
, getSFDCUserName
, getSFDCPassword
, respectively.