From ee62b10f6bf12e809285d926a9431d9a2d731cea Mon Sep 17 00:00:00 2001 From: Gialandro Date: Thu, 19 Apr 2018 10:21:06 -0500 Subject: [PATCH 1/8] Changes to Python 3.x.x --- .gitignore | 1 + elf.py | 79 +++++++++++++++++++++++++++++++++--------------------- 2 files changed, 50 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index df2929e..eeac48d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .csv +.vscode/ \ No newline at end of file diff --git a/elf.py b/elf.py index 5b1299d..e0ee98e 100755 --- a/elf.py +++ b/elf.py @@ -43,7 +43,7 @@ #Imports -import urllib2 +import urllib.request import json #import ssl import getpass @@ -51,14 +51,14 @@ import sys import gzip import time -from StringIO import StringIO +from io import StringIO import base64 # login function def login(): ''' Login to salesforce service using OAuth2 ''' # prompt for username and password - username = raw_input('Username: \n') + username = input('Username: \n') password = getpass.getpass('Password: \n') # check to see if anything was entered and if not, default values @@ -66,14 +66,18 @@ def login(): if len(username) < 1: username = 'user@company.com' password = 'Passw0rd' - print 'Using default username: ' + username + # print 'Using default username: ' + username #2.7.9 + print('Using default username: {0}'.format(username)) else: - print 'Using user inputed username: ' + username + # print 'Using user inputed username: ' + username #2.7.9 + print('Using user inputed username: {0}'.format(username)) - print 'check point' + # print 'check point' #2.7.9 + print('check point') # create a new salesforce REST API OAuth request url = 'https://login.salesforce.com/services/oauth2/token' - data = '&grant_type=password&client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&username='+username+'&password='+password + dataUnencoded = '&grant_type=password&client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&username='+username+'&password='+password + data = urllib.parse.urlencode(dataUnencoded).encode("utf-8") headers = {'X-PrettyPrint' : '1'} # workaround to ssl issue introduced before version 2.7.9 @@ -93,8 +97,11 @@ def login(): # urllib2.install_opener(opener) # call salesforce REST API and pass in OAuth credentials - req = urllib2.Request(url, data, headers) - res = urllib2.urlopen(req) + req = urllib.request.Request(url, data, headers) + try: + res = urllib.request.urlopen(req) + except urllib.error.URLError as e: + print('Error: {0}'.format(e.reason)) # load results to dictionary res_dict = json.load(res) @@ -114,20 +121,22 @@ def download_elf(): # login and retrieve access_token and day access_token, instance_url = login() - day = raw_input('\nDate range (e.g. Last_n_Days:2, Today, Tomorrow):\n') + day = input('\nDate range (e.g. Last_n_Days:2, Today, Tomorrow):\n') # check to see if anything was entered and if not, default values if len(day) < 1: day = 'Last_n_Days:2' - print 'Using default date range: ' + day + '\n' + # print 'Using default date range: ' + day + '\n' #2.7.9 + print('Using default date range: {0} \n'.format(day)) else: - print 'Using user inputed date range: ' + day + '\n' + # print 'Using user inputed date range: ' + day + '\n' #2.7.9 + print('Using user inputed date range: {0} \n'.format(day)) # query Ids from Event Log File url = instance_url+'/services/data/v33.0/query?q=SELECT+Id+,+EventType+,+Logdate+From+EventLogFile+Where+LogDate+=+'+day headers = {'Authorization' : 'Bearer ' + access_token, 'X-PrettyPrint' : '1'} - req = urllib2.Request(url, None, headers) - res = urllib2.urlopen(req) + req = urllib.request.Request(url, None, headers) + res = urllib.request.urlopen(req) res_dict = json.load(res) # capture record result size to loop over @@ -135,18 +144,21 @@ def download_elf(): # provide feedback if no records are returned if total_size < 1: - print 'No records were returned for ' + day + # print 'No records were returned for ' + day #2.7.9 + print('No records were returned for {0}'.format(day)) sys.exit() # create a directory for the output - dir = raw_input("Output directory: ") + dir = input("Output directory: ") # check to see if anything if len(dir) < 1: dir = 'elf' - print '\ndefault directory name used: ' + dir + # print '\ndefault directory name used: ' + dir #2.7.9 + print('\ndefault directory name used: {0}'.format(dir)) else: - print '\ndirectory name used: ' + dir + # print '\ndirectory name used: ' + dir #2.7.9 + print('\ndirectory name used: {0}'.format(dir)) # If directory doesn't exist, create one if not os.path.exists(dir): @@ -156,15 +168,17 @@ def download_elf(): res.close # check to see if the user wants to download it compressed - compress = raw_input('\nUse compression (y/n)\n').lower() - print compress + compress = input('\nUse compression (y/n)\n').lower() + print(compress) # check to see if anything if len(compress) < 1: compress = 'yes' - print '\ndefault compression being used: ' + compress + # print '\ndefault compression being used: ' + compress #2.7.9 + print('\ndefault compression being used: {0}'.format(compress)) else: - print '\ncompression being used: ' + compress + # print '\ncompression being used: ' + compress #2.7.9 + print('\ncompression being used: {0}'.format(compress)) # loop over json elements in result and download each file locally for i in range(total_size): @@ -179,22 +193,26 @@ def download_elf(): # provide correct compression header if (compress == 'y') or (compress == 'yes'): headers = {'Authorization' : 'Bearer ' + access_token, 'X-PrettyPrint' : '1', 'Accept-encoding' : 'gzip'} - print 'Using gzip compression\n' + # print 'Using gzip compression\n' #2.7.9 + print('Using gzip compression\n') else: headers = {'Authorization' : 'Bearer ' + access_token, 'X-PrettyPrint' : '1'} - print 'Not using gzip compression\n' + # print 'Not using gzip compression\n' #2.7.9 + print('Not using gzip compression\n') # begin profiling start = time.time() # open connection - req = urllib2.Request(url, None, headers) - res = urllib2.urlopen(req) + req = urllib.request.Request(url, None, headers) + res = urllib.request.urlopen(req) - print '********************************' + # print '********************************' #2.7.9 + print('********************************') # provide feedback to user - print 'Downloading: ' + dates[:10] + '-' + types + '.csv to ' + os.getcwd() + '/' + dir + '\n' + # print 'Downloading: ' + dates[:10] + '-' + types + '.csv to ' + os.getcwd() + '/' + dir + '\n' #2.7.9 + print('Downloading: ' + dates[:10] + '-' + types + '.csv to ' + os.getcwd() + '/' + dir + '\n') # print the response to see the content type # print res.info() @@ -228,7 +246,8 @@ def download_elf(): #msecs = secs * 1000 # millisecs #print 'elapsed time: %f ms' % msecs - print 'Total download time: %f seconds\n' % secs + # print 'Total download time: %f seconds\n' % secs #2.7.9 + print('Total download time: %f seconds\n' % secs) file.close i = i + 1 @@ -236,4 +255,4 @@ def download_elf(): # close connection res.close -download_elf() +download_elf() \ No newline at end of file From 68ed5204c72805d3bb4a489cd7d6115907596bd1 Mon Sep 17 00:00:00 2001 From: Gialandro Date: Thu, 19 Apr 2018 11:50:46 -0500 Subject: [PATCH 2/8] Format of dataUnencoded changed and gitignore changed --- .gitignore | 3 ++- elf.py | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index eeac48d..4d084a2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .csv -.vscode/ \ No newline at end of file +.vscode/ +.DS_Store \ No newline at end of file diff --git a/elf.py b/elf.py index e0ee98e..0c5157d 100755 --- a/elf.py +++ b/elf.py @@ -38,8 +38,8 @@ # Connected App information: fill it in by creating a connected app # https://help.salesforce.com/articleView?id=connected_app_create.htm&language=en_US&type=0 -CLIENT_ID = 'FILL_ME_IN' -CLIENT_SECRET = 'FILL_ME_IN' +CLIENT_ID = '3MVG9CEn_O3jvv0xxArwB5GMmzT8s9RSd5RVX6OGYAzQouo9ON.xBk4agjNLoUUUhVbn2SqiTCxWfRJ5_8Hc_' +CLIENT_SECRET = '73446011160915447' #Imports @@ -76,7 +76,8 @@ def login(): print('check point') # create a new salesforce REST API OAuth request url = 'https://login.salesforce.com/services/oauth2/token' - dataUnencoded = '&grant_type=password&client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&username='+username+'&password='+password + # dataUnencoded = '&grant_type=password&client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&username='+username+'&password='+password #2.7.9 + dataUnencoded = {'grant_type': 'password', 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'username': username, 'password': password} data = urllib.parse.urlencode(dataUnencoded).encode("utf-8") headers = {'X-PrettyPrint' : '1'} @@ -133,7 +134,7 @@ def download_elf(): print('Using user inputed date range: {0} \n'.format(day)) # query Ids from Event Log File - url = instance_url+'/services/data/v33.0/query?q=SELECT+Id+,+EventType+,+Logdate+From+EventLogFile+Where+LogDate+=+'+day + url = instance_url+'/services/data/v41.0/query?q=SELECT+Id+,+EventType+,+Logdate+From+EventLogFile+Where+LogDate+=+'+day headers = {'Authorization' : 'Bearer ' + access_token, 'X-PrettyPrint' : '1'} req = urllib.request.Request(url, None, headers) res = urllib.request.urlopen(req) @@ -188,7 +189,7 @@ def download_elf(): dates = res_dict['records'][i]['LogDate'] # create REST API request - url = instance_url+'/services/data/v33.0/sobjects/EventLogFile/'+ids+'/LogFile' + url = instance_url+'/services/data/v41.0/sobjects/EventLogFile/'+ids+'/LogFile' # provide correct compression header if (compress == 'y') or (compress == 'yes'): From f1eda8a2d2f13621c412f6b200688e438a5cc9ea Mon Sep 17 00:00:00 2001 From: Gialandro Date: Wed, 9 May 2018 12:42:10 -0500 Subject: [PATCH 3/8] installed 'requests' in python3... Modified credentials --- elf.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/elf.py b/elf.py index 0c5157d..3f78ada 100755 --- a/elf.py +++ b/elf.py @@ -38,12 +38,13 @@ # Connected App information: fill it in by creating a connected app # https://help.salesforce.com/articleView?id=connected_app_create.htm&language=en_US&type=0 -CLIENT_ID = '3MVG9CEn_O3jvv0xxArwB5GMmzT8s9RSd5RVX6OGYAzQouo9ON.xBk4agjNLoUUUhVbn2SqiTCxWfRJ5_8Hc_' -CLIENT_SECRET = '73446011160915447' +CLIENT_ID = '3MVG9CEn_O3jvv0xxArwB5GMmgutvtGbnMfsFiwiO75Xu8S0FL.5RTLHugnunoufsaww68QllkJopbffv.y1' +CLIENT_SECRET = '59596969495969775440' #Imports import urllib.request +import requests import json #import ssl import getpass @@ -64,8 +65,8 @@ def login(): # check to see if anything was entered and if not, default values # change default values for username and password to your own if len(username) < 1: - username = 'user@company.com' - password = 'Passw0rd' + username = 'mail@test.com' + password = 'PasswordToken' # print 'Using default username: ' + username #2.7.9 print('Using default username: {0}'.format(username)) else: @@ -77,7 +78,14 @@ def login(): # create a new salesforce REST API OAuth request url = 'https://login.salesforce.com/services/oauth2/token' # dataUnencoded = '&grant_type=password&client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&username='+username+'&password='+password #2.7.9 - dataUnencoded = {'grant_type': 'password', 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'username': username, 'password': password} + dataUnencoded = { + 'grant_type': 'password', + 'client_id': CLIENT_ID, + 'client_secret': CLIENT_SECRET, + 'redirect_uri': 'https://localhost/', + 'username': username, + 'password': password + } data = urllib.parse.urlencode(dataUnencoded).encode("utf-8") headers = {'X-PrettyPrint' : '1'} @@ -99,10 +107,10 @@ def login(): # call salesforce REST API and pass in OAuth credentials req = urllib.request.Request(url, data, headers) - try: - res = urllib.request.urlopen(req) - except urllib.error.URLError as e: - print('Error: {0}'.format(e.reason)) + # try: + res = urllib.request.urlopen(req) + # except urllib.error.URLError as e: + # print('Error: {0}'.format(e.reason)) # load results to dictionary res_dict = json.load(res) From 7f25b92e3838b408d93bfa24f49a0bd0d1cbe285 Mon Sep 17 00:00:00 2001 From: Gialandro Date: Wed, 9 May 2018 13:11:11 -0500 Subject: [PATCH 4/8] Response decoded, credentials required (Consumer key and Consumer Secret --- elf.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/elf.py b/elf.py index 3f78ada..65817c7 100755 --- a/elf.py +++ b/elf.py @@ -38,8 +38,6 @@ # Connected App information: fill it in by creating a connected app # https://help.salesforce.com/articleView?id=connected_app_create.htm&language=en_US&type=0 -CLIENT_ID = '3MVG9CEn_O3jvv0xxArwB5GMmgutvtGbnMfsFiwiO75Xu8S0FL.5RTLHugnunoufsaww68QllkJopbffv.y1' -CLIENT_SECRET = '59596969495969775440' #Imports @@ -61,17 +59,21 @@ def login(): # prompt for username and password username = input('Username: \n') password = getpass.getpass('Password: \n') + clientId = input('Consumer key: \n') + clientSecret = input('Consumer secret: \n') # check to see if anything was entered and if not, default values # change default values for username and password to your own if len(username) < 1: - username = 'mail@test.com' + username = 'your@email.com' password = 'PasswordToken' + clientId = 'YourConsumerKey' + clientSecret = 'YourConsumerSecret' # print 'Using default username: ' + username #2.7.9 - print('Using default username: {0}'.format(username)) + print('Using default username and credentials: {0}'.format(username)) else: # print 'Using user inputed username: ' + username #2.7.9 - print('Using user inputed username: {0}'.format(username)) + print('Using user inputed username and credentials: {0}'.format(username)) # print 'check point' #2.7.9 print('check point') @@ -80,9 +82,9 @@ def login(): # dataUnencoded = '&grant_type=password&client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&username='+username+'&password='+password #2.7.9 dataUnencoded = { 'grant_type': 'password', - 'client_id': CLIENT_ID, - 'client_secret': CLIENT_SECRET, - 'redirect_uri': 'https://localhost/', + 'client_id': clientId, + 'client_secret': clientSecret, + 'redirect_uri': 'YourRedirectURI', 'username': username, 'password': password } @@ -230,7 +232,7 @@ def download_elf(): # compression code from http://bit.ly/pyCompression if res.info().get('Content-Encoding') == 'gzip': # buffer results - buf = StringIO(res.read()) + buf = StringIO(res.read().decode('utf-8')) # gzip decode the response f = gzip.GzipFile(fileobj=buf) # print data @@ -239,7 +241,7 @@ def download_elf(): buf.close() else: # buffer results - buf = StringIO(res.read()) + buf = StringIO(res.read().decode('utf-8')) # get the value from the buffer data = buf.getvalue() #print data From f37eb525bcfc702a0c72b242bae23439a135b48e Mon Sep 17 00:00:00 2001 From: Gialandro Date: Fri, 11 May 2018 13:33:55 -0500 Subject: [PATCH 5/8] Personal changes... --- elf.py | 113 ++++++++++++++------------------------------------------- 1 file changed, 27 insertions(+), 86 deletions(-) diff --git a/elf.py b/elf.py index 65817c7..285461d 100755 --- a/elf.py +++ b/elf.py @@ -1,50 +1,10 @@ -#!/usr/bin/python -''' -# Python 2.7.9 script to download EventLogFiles -# Pre-requisite: standard library functionality = e.g urrlib2, json, StringIO - - #/** - #* Copyright (c) 2012, Salesforce.com, Inc. All rights reserved. - #* - #* Redistribution and use in source and binary forms, with or without - #* modification, are permitted provided that the following conditions are - #* met: - #* - #* * Redistributions of source code must retain the above copyright - #* notice, this list of conditions and the following disclaimer. - #* - #* * Redistributions in binary form must reproduce the above copyright - #* notice, this list of conditions and the following disclaimer in - #* the documentation and/or other materials provided with the - #* distribution. - #* - #* * Neither the name of Salesforce.com nor the names of its - #* contributors may be used to endorse or promote products derived - #* from this software without specific prior written permission. - #* - #* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - #* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - #* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - #* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - #* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - #* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - #* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - #* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - #* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - #* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - #* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - #*/ -''' # Connected App information: fill it in by creating a connected app # https://help.salesforce.com/articleView?id=connected_app_create.htm&language=en_US&type=0 #Imports - import urllib.request -import requests import json -#import ssl import getpass import os import sys @@ -56,30 +16,34 @@ # login function def login(): ''' Login to salesforce service using OAuth2 ''' - # prompt for username and password - username = input('Username: \n') - password = getpass.getpass('Password: \n') - clientId = input('Consumer key: \n') - clientSecret = input('Consumer secret: \n') + # username = '' + # password = '' + # clientId = '' + # clientSecret = '' + # prompt for username, password, consumer key and consumer secret + # while len(username) < 1: + username = input('Username: ') + # while len(password) < 1: + password = getpass.getpass('Password:') + # while len(clientId) < 1: + clientId = input('Consumer key: ') + # while len(clientSecret) < 1: + clientSecret = input('Consumer secret: ') # check to see if anything was entered and if not, default values # change default values for username and password to your own if len(username) < 1: - username = 'your@email.com' + username = 'you@mail.com' password = 'PasswordToken' - clientId = 'YourConsumerKey' - clientSecret = 'YourConsumerSecret' - # print 'Using default username: ' + username #2.7.9 + clientId = 'Yourclientid' + clientSecret = 'Yourclientsecret' print('Using default username and credentials: {0}'.format(username)) else: - # print 'Using user inputed username: ' + username #2.7.9 print('Using user inputed username and credentials: {0}'.format(username)) - # print 'check point' #2.7.9 print('check point') # create a new salesforce REST API OAuth request url = 'https://login.salesforce.com/services/oauth2/token' - # dataUnencoded = '&grant_type=password&client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&username='+username+'&password='+password #2.7.9 dataUnencoded = { 'grant_type': 'password', 'client_id': clientId, @@ -90,25 +54,8 @@ def login(): } data = urllib.parse.urlencode(dataUnencoded).encode("utf-8") headers = {'X-PrettyPrint' : '1'} - - # workaround to ssl issue introduced before version 2.7.9 - #if hasattr(ssl, '_create_unverified_context'): - #ssl._create_default_https_context = ssl._create_unverified_context - - # These lines are for when you have a proxy server - # uncomment the next line to work with a local proxy server. replace the URL with the URL of your proxy - # proxy = urllib2.ProxyHandler({'https': 'http://127.0.0.1:8888/'}) - # uncomment the next two lines if your proxy needs authentication. replace 'realm' through 'password' with appropriate values. 'realm' is often null - # proxy_auth_handler = urllib2.HTTPBasicAuthHandler() - # proxy_auth_handler.add_password('realm', 'host', 'username', 'password') - # pick one of the next two lines based on whether you need proxy authentication. The first is authenticated, the second is unauthenticated - # opener = urllib2.build_opener(proxy, proxy_auth_handler) - # opener = urllib2.build_opener(proxy) - # uncomment the final line to enable the proxy for any calls - # urllib2.install_opener(opener) - # call salesforce REST API and pass in OAuth credentials - req = urllib.request.Request(url, data, headers) + req = urllib.request.Request(url, data, headers = headers) # try: res = urllib.request.urlopen(req) # except urllib.error.URLError as e: @@ -137,16 +84,14 @@ def download_elf(): # check to see if anything was entered and if not, default values if len(day) < 1: day = 'Last_n_Days:2' - # print 'Using default date range: ' + day + '\n' #2.7.9 print('Using default date range: {0} \n'.format(day)) else: - # print 'Using user inputed date range: ' + day + '\n' #2.7.9 print('Using user inputed date range: {0} \n'.format(day)) # query Ids from Event Log File url = instance_url+'/services/data/v41.0/query?q=SELECT+Id+,+EventType+,+Logdate+From+EventLogFile+Where+LogDate+=+'+day headers = {'Authorization' : 'Bearer ' + access_token, 'X-PrettyPrint' : '1'} - req = urllib.request.Request(url, None, headers) + req = urllib.request.Request(url, None, headers = headers) res = urllib.request.urlopen(req) res_dict = json.load(res) @@ -155,7 +100,6 @@ def download_elf(): # provide feedback if no records are returned if total_size < 1: - # print 'No records were returned for ' + day #2.7.9 print('No records were returned for {0}'.format(day)) sys.exit() @@ -165,10 +109,8 @@ def download_elf(): # check to see if anything if len(dir) < 1: dir = 'elf' - # print '\ndefault directory name used: ' + dir #2.7.9 print('\ndefault directory name used: {0}'.format(dir)) else: - # print '\ndirectory name used: ' + dir #2.7.9 print('\ndirectory name used: {0}'.format(dir)) # If directory doesn't exist, create one @@ -179,16 +121,16 @@ def download_elf(): res.close # check to see if the user wants to download it compressed - compress = input('\nUse compression (y/n)\n').lower() - print(compress) + # compress = input('\nUse compression (y/n)\n').lower() + # print(compress) + # Disabled compress validation meanwhile... + compress = 'n' # check to see if anything if len(compress) < 1: compress = 'yes' - # print '\ndefault compression being used: ' + compress #2.7.9 print('\ndefault compression being used: {0}'.format(compress)) else: - # print '\ncompression being used: ' + compress #2.7.9 print('\ncompression being used: {0}'.format(compress)) # loop over json elements in result and download each file locally @@ -204,11 +146,9 @@ def download_elf(): # provide correct compression header if (compress == 'y') or (compress == 'yes'): headers = {'Authorization' : 'Bearer ' + access_token, 'X-PrettyPrint' : '1', 'Accept-encoding' : 'gzip'} - # print 'Using gzip compression\n' #2.7.9 print('Using gzip compression\n') else: headers = {'Authorization' : 'Bearer ' + access_token, 'X-PrettyPrint' : '1'} - # print 'Not using gzip compression\n' #2.7.9 print('Not using gzip compression\n') # begin profiling @@ -218,11 +158,9 @@ def download_elf(): req = urllib.request.Request(url, None, headers) res = urllib.request.urlopen(req) - # print '********************************' #2.7.9 print('********************************') # provide feedback to user - # print 'Downloading: ' + dates[:10] + '-' + types + '.csv to ' + os.getcwd() + '/' + dir + '\n' #2.7.9 print('Downloading: ' + dates[:10] + '-' + types + '.csv to ' + os.getcwd() + '/' + dir + '\n') # print the response to see the content type @@ -232,7 +170,11 @@ def download_elf(): # compression code from http://bit.ly/pyCompression if res.info().get('Content-Encoding') == 'gzip': # buffer results - buf = StringIO(res.read().decode('utf-8')) + html = res.read() + decodedHtml = html.decode('iso-8859-1')#.encode('UTF-8') + # decodedHtml = decodedHtml.decode('UTF-8') + print('HTML=>', decodedHtml) + buf = StringIO(decodedHtml) # gzip decode the response f = gzip.GzipFile(fileobj=buf) # print data @@ -257,7 +199,6 @@ def download_elf(): #msecs = secs * 1000 # millisecs #print 'elapsed time: %f ms' % msecs - # print 'Total download time: %f seconds\n' % secs #2.7.9 print('Total download time: %f seconds\n' % secs) file.close From b296c513c98338cd26e4f3358d035930c927d374 Mon Sep 17 00:00:00 2001 From: Gialandro Date: Wed, 16 May 2018 12:02:11 -0500 Subject: [PATCH 6/8] Added input to use Login or Test url, handled Bad Request Error --- elf.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/elf.py b/elf.py index 285461d..aff5cfc 100755 --- a/elf.py +++ b/elf.py @@ -22,6 +22,7 @@ def login(): # clientSecret = '' # prompt for username, password, consumer key and consumer secret # while len(username) < 1: + print('Remember, you need data from a Connected App') username = input('Username: ') # while len(password) < 1: password = getpass.getpass('Password:') @@ -29,6 +30,7 @@ def login(): clientId = input('Consumer key: ') # while len(clientSecret) < 1: clientSecret = input('Consumer secret: ') + instanceType = input('Instance type (login by default): ') # check to see if anything was entered and if not, default values # change default values for username and password to your own @@ -40,10 +42,13 @@ def login(): print('Using default username and credentials: {0}'.format(username)) else: print('Using user inputed username and credentials: {0}'.format(username)) + # Use 'login' by default + if len(instanceType) < 1: + instanceType = 'login' print('check point') # create a new salesforce REST API OAuth request - url = 'https://login.salesforce.com/services/oauth2/token' + url = 'https://' + instanceType + '.salesforce.com/services/oauth2/token' dataUnencoded = { 'grant_type': 'password', 'client_id': clientId, @@ -56,16 +61,14 @@ def login(): headers = {'X-PrettyPrint' : '1'} # call salesforce REST API and pass in OAuth credentials req = urllib.request.Request(url, data, headers = headers) - # try: - res = urllib.request.urlopen(req) - # except urllib.error.URLError as e: - # print('Error: {0}'.format(e.reason)) - - # load results to dictionary - res_dict = json.load(res) - - # close connection - res.close() + try: + res = urllib.request.urlopen(req) + except urllib.error.URLError as e: + if(e.reason == 'Bad Request'): + print('Error: {0}, check input data'.format(e.reason)) + else: + print('Error: {0}'.format(e.reason)) + sys.exit() # return OAuth access token necessary for additional REST API calls access_token = res_dict['access_token'] From c4c2c0f9c2f74318b47b14995ecb314ce99338a6 Mon Sep 17 00:00:00 2001 From: Gialandro Date: Wed, 16 May 2018 12:50:06 -0500 Subject: [PATCH 7/8] Added input for Redirect URI(from App Connected) --- elf.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/elf.py b/elf.py index aff5cfc..fb92e15 100755 --- a/elf.py +++ b/elf.py @@ -25,20 +25,22 @@ def login(): print('Remember, you need data from a Connected App') username = input('Username: ') # while len(password) < 1: - password = getpass.getpass('Password:') + password = getpass.getpass('Password and Token:') # while len(clientId) < 1: clientId = input('Consumer key: ') # while len(clientSecret) < 1: clientSecret = input('Consumer secret: ') instanceType = input('Instance type (login by default): ') + redirectURI = input('Redirect URI (from your App Connected, "http://localhost:4200/" by default): ') # check to see if anything was entered and if not, default values # change default values for username and password to your own if len(username) < 1: - username = 'you@mail.com' + username = 'your@email.com' password = 'PasswordToken' - clientId = 'Yourclientid' - clientSecret = 'Yourclientsecret' + clientId = 'YourConsumerKey' + clientSecret = 'YourClientSecret' + redirectURI = 'http://localhost:4200/' print('Using default username and credentials: {0}'.format(username)) else: print('Using user inputed username and credentials: {0}'.format(username)) @@ -53,7 +55,7 @@ def login(): 'grant_type': 'password', 'client_id': clientId, 'client_secret': clientSecret, - 'redirect_uri': 'YourRedirectURI', + 'redirect_uri': redirectURI, 'username': username, 'password': password } @@ -63,6 +65,8 @@ def login(): req = urllib.request.Request(url, data, headers = headers) try: res = urllib.request.urlopen(req) + res_dict = json.load(res) + res.close() except urllib.error.URLError as e: if(e.reason == 'Bad Request'): print('Error: {0}, check input data'.format(e.reason)) From 1d6b869b3fea047ee308e52b15c95a2784ebe0b0 Mon Sep 17 00:00:00 2001 From: Gialandro Date: Wed, 16 May 2018 13:25:09 -0500 Subject: [PATCH 8/8] Added SSL context --- elf.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/elf.py b/elf.py index fb92e15..5d6f786 100755 --- a/elf.py +++ b/elf.py @@ -5,6 +5,7 @@ #Imports import urllib.request import json +import ssl import getpass import os import sys @@ -64,7 +65,8 @@ def login(): # call salesforce REST API and pass in OAuth credentials req = urllib.request.Request(url, data, headers = headers) try: - res = urllib.request.urlopen(req) + ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) + res = urllib.request.urlopen(req, context = ctx) res_dict = json.load(res) res.close() except urllib.error.URLError as e: