Skip to content

Instantly share code, notes, and snippets.

@madewulf
Created May 18, 2017 13:32
Show Gist options
  • Save madewulf/d1994e61f1f6d482d5fdfc4d946b3c56 to your computer and use it in GitHub Desktop.
Save madewulf/d1994e61f1f6d482d5fdfc4d946b3c56 to your computer and use it in GitHub Desktop.
Python Flask and the Force.com REST API: Simple simple-salesforce example
from flask import Flask, redirect, request
import requests
from simple_salesforce import Salesforce
consumer_key = 'your_consumer_key'
consumer_secret = 'your_consumer_secret'
access_token_url = 'https://login.salesforce.com/services/oauth2/token'
redirect_uri = 'http://localhost:5000/callback'
authorize_url = 'https://login.salesforce.com/services/oauth2/authorize'
app = Flask(__name__)
@app.route('/login')
def login():
url = "%s?response_type=code&client_id=%s&redirect_uri=%s" % (authorize_url, consumer_key, redirect_uri)
return redirect(url)
@app.route('/callback')
def callback():
code = request.args.get('code')
data = {
'grant_type': 'authorization_code',
'redirect_uri': redirect_uri,
'code': code,
'client_id': consumer_key,
'client_secret': consumer_secret
}
headers = {
'content-type': 'application/x-www-form-urlencoded'
}
req = requests.post(access_token_url, data=data, headers=headers)
response = req.json()
sf = Salesforce(instance_url=response['instance_url'], session_id=response['access_token'])
#Now you can make your calls to the Salesforce api using the sf object. For more information: https://github.com/simple-salesforce/simple-salesforce
return "ok, connected to Salesforce"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment