Skip to content

Instantly share code, notes, and snippets.

@bpux
Last active February 11, 2016 02:06
Show Gist options
  • Save bpux/477607fa6cd20493af98 to your computer and use it in GitHub Desktop.
Save bpux/477607fa6cd20493af98 to your computer and use it in GitHub Desktop.

Revisions

  1. @joshbirk joshbirk revised this gist Jan 13, 2014. 1 changed file with 12 additions and 1 deletion.
    13 changes: 12 additions & 1 deletion force-oauth.py
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,9 @@
    import json
    from simple_salesforce import Salesforce

    #login here:
    #https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=3MVG9A2kN3Bn17hsWsLDatw._IVMEUBoPKv.7ksp0tz7xLX4tWDVgyzwTCA7i_yTfP.qYuNOsSoPNcdVH6DuE&redirect_uri=http://localhost/cgi-bin/python/oauth.py

    consumer_key = '3MVG9A2kN3Bn17hsWsLDatw._IVMEUBoPKv.7ksp0tz7xLX4tWDVgyzwTCA7i_yTfP.qYuNOsSoPNcdVH6DuE'
    consumer_secret = '8779811613588378217'
    request_token_url = 'https://login.salesforce.com/services/oauth2/token'
    @@ -14,6 +17,12 @@

    query = cgi.FieldStorage()
    req = None


    if 'login' in query:
    print "Location: https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id="+consumer_key+"&redirect_uri="+redirect_uri
    print

    if 'code' in query:
    code = query.getvalue('code')

    @@ -31,7 +40,8 @@
    response = req.json()
    sf = Salesforce(instance_url=response['instance_url'], session_id=response['access_token'])
    records = sf.query("SELECT Id, Name, Email FROM Contact")
    records = records['records'];
    records = records['records']

    #print web page
    print "Content-type: text/html"
    print
    @@ -45,4 +55,5 @@
    print "<tr><td>"+record['Name']+"</td><td>"+record['Email']+"</td></tr>"

    print "</table>"

    print "</body></html>"
  2. @joshbirk joshbirk created this gist Jan 10, 2014.
    48 changes: 48 additions & 0 deletions force-oauth.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/usr/bin/python

    import cgi
    import requests
    import json
    from simple_salesforce import Salesforce

    consumer_key = '3MVG9A2kN3Bn17hsWsLDatw._IVMEUBoPKv.7ksp0tz7xLX4tWDVgyzwTCA7i_yTfP.qYuNOsSoPNcdVH6DuE'
    consumer_secret = '8779811613588378217'
    request_token_url = 'https://login.salesforce.com/services/oauth2/token'
    access_token_url = 'https://login.salesforce.com/services/oauth2/token'
    redirect_uri = 'http://localhost/cgi-bin/python/oauth.py'
    authorize_url = 'https://login.salesforce.com/services/oauth2/authorize' #?response_type=token&client_id='+consumer_key+'&redirect_uri='+redirect_uri

    query = cgi.FieldStorage()
    req = None
    if 'code' in query:
    code = query.getvalue('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'])
    records = sf.query("SELECT Id, Name, Email FROM Contact")
    records = records['records'];
    #print web page
    print "Content-type: text/html"
    print

    print "<html><body>"
    print "<h1>SELECT Id, Name, Email FROM Contact</h1>"

    print "<table>"
    print "<tr><td><b>Name</b></td><td><b>Email</b></td></tr>"
    for record in records:
    print "<tr><td>"+record['Name']+"</td><td>"+record['Email']+"</td></tr>"

    print "</table>"
    print "</body></html>"