Skip to content

Instantly share code, notes, and snippets.

@scottpersinger
Created February 5, 2014 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottpersinger/8831801 to your computer and use it in GitHub Desktop.
Save scottpersinger/8831801 to your computer and use it in GitHub Desktop.
def salesforce_oauth(s, oauth_url, username=None, password=None):
"""s should be a requests session"""
r = s.get(oauth_url)
params = urlparse.parse_qs(urlparse.urlparse(r.url).query)
data = {"un":username,
"width":2560,
"height":1440,
"hasRememberUn":True,
"startURL":params['startURL'],
"loginURL":"",
"loginType":6,
"useSecure":True,
"local":"",
"lt":"OAUTH",
"qs":"r=https%3A%2F%2Flocalhost%3A8443%2Fsalesforce%2F21",
"locale":"",
"oauth_token":"",
"oauth_callback":"",
"login":"",
"serverid":"",
"display":"popup",
"username":username,
"pw":password,
"Login":""}
r2 = s.post("https://login.salesforce.com", data)
m = re.search("window.location.href\s*='(.[^']+)'", r2.text)
assert m is not None, "Couldn't find location.href expression in page %s:\n%s" % (r2.url, r2.text)
u3 = "https://" + urlparse.urlparse(r2.url).hostname + m.group(1)
r3 = s.get(u3)
m = re.search("window.location.href\s*='(.[^']+)'", r3.text)
assert m is not None, "Couldn't find location.href expression in page %s:\n%s" % (r3.url, r3.text)
return m.group(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment