salesforce-marketingcloud / FuelSDK-Python

FuelSDK for python
Other
126 stars 193 forks source link

ET_Client() PROXY AUTHENTICATION #118

Open tgtimoteo opened 4 years ago

tgtimoteo commented 4 years ago

I'm trying to instanciate the ET_Client and get the 407 error... it's proxy authenticatio

How do I configure this class to read my proxies authentication?

Gexar commented 3 years ago

I am behind a corporate firewall, which means that my proxy needs login and password and here is how I did it (replace clientid, clientsecret, id, login, password and host with your values):

import os, ET_Client

login = "YOUR DATA GOES HERE"
password = "YOUR DATA GOES HERE"
proxy_host = "YOUR DATA GOES HERE"
clientid = "YOUR DATA GOES HERE"
clientsecret = "YOUR DATA GOES HERE"
authurl = "YOUR DATA GOES HERE"

proxy = f'http://{login}:{password}@{proxy_host}:8080'

os.environ['http_proxy'] = proxy 
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy

print("Trying to log in...")
stubObj = ET_Client.ET_Client(False, False,
                             params={'clientid': clientid,
                                     "clientsecret":clientsecret,
                                     "useOAuth2Authentication":"True",
                                     "authenticationurl":authurl})
print("Login: DONE!")

proxy_settings = dict(http=proxy, https=proxy)
stubObj.soap_client.set_options(proxy=proxy_settings)

print('>>> Get all of the DataExtensions in an Account')
de = ET_Client.ET_DataExtension()
print('>>> Get all of the DataExtensions in an Account: DONE!')
de.auth_stub = stubObj
de.props = ["CustomerKey", "Name"]

print('>>> Fetching extensions\' data')
getResponse = de.get()
print(f'>>> Fetching extensions\' data: DONE!')
print(f'Retrieve Status: {getResponse.status}')
print(f'Code: {getResponse.code}')
print(f'Message: {getResponse.message}')
print(f'MoreResults: {getResponse.more_results}')
print(f'RequestID: {getResponse.request_id}')
print(f'Results Length: {len(getResponse.results)}')

Update: for the underlying soap client to use your proxy settings you need to specify them in the build_soap_client() method defined in FuelSDK/client.py at line 245. This is how I do it: 1)Add line

proxy_settings = dict(http=os.environ["HTTP_PROXY"], https=os.environ["HTTPS_PROXY"])

2)replace

self.soap_client = suds.client.Client(self.wsdl_file_url, faults=False, cachingpolicy=1)

with

self.soap_client = suds.client.Client(self.wsdl_file_url, faults=False, cachingpolicy=1, proxy=proxy_settings)

Edit: added new info, the update