heroku / salesforce-oauth-request

Util package to drive Salesforce Oauth Web flow for scripting and testing.
MIT License
34 stars 12 forks source link

Project does not work on windows. Library does not work with test salesforce environment #1

Open lstrupinskaya opened 10 years ago

lstrupinskaya commented 10 years ago

Hi Scott,

Thank you for creating this module. It was very helpful to me.

I have found two issues with it that I fixed locally in my download and would like to have them incorporated into main library.

  1. this library does not work on windows, because there is no notion of HOME variable.
  2. Library does not work properly with test environment of salesforce when sandbox flag is set to true.

Below is modifications I made to the code that could be incorporated into code to fix these issues:

if not os.environ.has_key('HOME'):
    os.environ['HOME'] = os.path.join(os.environ['HOMEDRIVE'], \
        os.environ['HOMEPATH'])

inside website_login procedure I added a check for sandbox parameter and redirect to test.salesforce.com if it is true:

def website_login(username = None, password = None, client_id = None, client_secret = None, redirect_uri = None, sandbox = None, cache_session = None, state = None): base = "https://login.salesforce.com" if not sandbox else "https://test.salesforce.com" auth_url = base + "/services/oauth2/authorize?"

client_id = os.environ.get('SALESFORCE_CLIENT_ID', client_id)
client_secret = os.environ.get('SALESFORCE_CLIENT_SECRET', client_secret)
redirect_uri = os.environ.get('SALESFORCE_REDIRECT_URI', redirect_uri)

auth_url += urllib.urlencode([
    ("response_type", "code"), 
    ("display", "popup"), 
    ("client_id",client_id),
    ("redirect_uri", redirect_uri), 
    ("prompt", "login"), 
    ("state", state)])

s = requests.session()
redirect_return = oauth_flow(s, auth_url, username=username, password=password)

# parse out the session id and endpoint
params = urlparse.parse_qs(redirect_return)

data = dict(code=params['code'],
            grant_type="authorization_code",
            client_id=client_id,
            client_secret=client_secret,
            redirect_uri=redirect_uri,
            format="json")

code_url = base + "/services/oauth2/token"
return requests.post(code_url, data=data)
aashay commented 10 years ago

I think #2 solved this