Git Product home page Git Product logo

salesforce-oauth-request's Introduction

Salesforce Oauth Request

A bit of Requests driven utility code to drive the Salesforce.com Oauth2 Web flow. Use this module to login to a Salesforce account using just the username and password. Good for tests and scripts which need Salesforce access_token access.

Installation

pip install salesforce-oauth-request

Usage

You need a Connected App defined in Salesforce so that you have all these pieces of info:

  1. client ID
  2. client secret
  3. redirect uri

Now you can get an access_token and instance_url like this:

import salesforce_oauth_request

result = salesforce_oauth_request.login(username=username,
										password=password,
										client_id=client_id,
										client_secret=client_secret,
										redirect_uri=redirect_uri)

print "Access token:  %s" + result['access_token']
print "Refresh token: %s" + result['refresh_token']
print "Instance URL:  %s" + result['instance_url']

# Now use access_token, instance_url for REST, SOAP, or BULK API access.

Options

cache_session=True/False - default False

Pass the option cache_session=true to save the access token to ~.sf_oauth to save time on repeated runs.

sandbox=True/False - default False

Pass sandbox=True to use a sandbox login.

Testing

You can run the simple test script like this:

python test.py

It will prompt you for all the values, drive the login, and print the results.

salesforce-oauth-request's People

Contributors

scottpersinger avatar svc-scm avatar vividboarder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

salesforce-oauth-request's Issues

Could't find location.href expression

The test even fails.

I get:

AssertionError: Couldn't find location.href expression in page https://cs15.salesforce.com/secur/frontdoor.jsp?sid=FOOOOOOOBAR (Username or password is wrong)

Clearly the username and password are correct as I am getting a frontdoor URL and my user login history shows a successful login. The library is just not properly parsing out sessions and tokens.

Is this project dead?

Is there another project taking over for this? No changes have been merged for a year.

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

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.