2

i've python coode retriving data from salesforce and it was working well, from a few days i was running the code but it didn't and show me an error message (TooManyRedirects: Exceeded 30 redirects), i tried to search the meaning of it but i didn't found any result

Python Code:-

start_time = datetime.now()

#Access to Salesforce
sf = Salesforce(username='-----',password='-----',security_token='-----')

# Salesforce Reports Paths
Accepted_Cases = '00O6N000000ZkQiUAK'
New_Case = '00O6N000000SkDaUAK'
WF_Case_Dispostions  = '00O6N000000SkDVUA0'
Total_Reply_by_Agent = '00O6N000000ZkQTUA0'
#Offered_Cases_T2 = '00O6N000000SqclUAC'

# Salesforce Reports
Accepted_Cases_df = pd.read_csv(StringIO(requests.get(f'https://altayyargroup.lightning.force.com/{Accepted_Cases}?isdtp=p1&export=1&enc=UTF-8&xf=csv', headers=sf.headers, cookies={'sid': sf.session_id}).content.decode('utf-8')))
New_Case_df = pd.read_csv(StringIO(requests.get(f'https://altayyargroup.lightning.force.com/{New_Case}?isdtp=p1&export=1&enc=UTF-8&xf=csv', headers=sf.headers, cookies={'sid': sf.session_id}).content.decode('utf-8')))
WF_Case_Dispostions_df = pd.read_csv(StringIO(requests.get(f'https://altayyargroup.lightning.force.com/{WF_Case_Dispostions}?isdtp=p1&export=1&enc=UTF-8&xf=csv', headers=sf.headers, cookies={'sid': sf.session_id}).content.decode('utf-8')))
Total_Reply_by_Agent_df = pd.read_csv(StringIO(requests.get(f'https://altayyargroup.lightning.force.com/{Total_Reply_by_Agent}?isdtp=p1&export=1&enc=UTF-8&xf=csv', headers=sf.headers, cookies={'sid': sf.session_id}).content.decode('utf-8')))
#Offered_Cases_T2_df = pd.read_csv(StringIO(requests.get(f'https://altayyargroup.lightning.force.com/{Offered_Cases_T2}?isdtp=p1&export=1&enc=UTF-8&xf=csv', headers=sf.headers, cookies={'sid': sf.session_id}).content.decode('utf-8')))

end_time = datetime.now()
print(f'Duration: {end_time - start_time}')

The Error Message:-

TooManyRedirects                          Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13988/64063902.py in <module>
     13 # Salesforce Reports
     14 #Accepted_Cases_df = pd.read_csv(StringIO(requests.get(f'https://altayyargroup.lightning.force.com/{Accepted_Cases}?isdtp=p1&export=1&enc=UTF-8&xf=csv', headers=sf.headers, cookies={'sid': sf.session_id}).content.decode('utf-8')))
---> 15 New_Case_df = pd.read_csv(StringIO(requests.get(f'https://altayyargroup.lightning.force.com/{New_Case}?isdtp=p1&export=1&enc=UTF-8&xf=csv', headers=sf.headers, cookies={'sid': sf.session_id}).content.decode('utf-8')))
     16 #WF_Case_Dispostions_df = pd.read_csv(StringIO(requests.get(f'https://altayyargroup.lightning.force.com/{WF_Case_Dispostions}?isdtp=p1&export=1&enc=UTF-8&xf=csv', headers=sf.headers, cookies={'sid': sf.session_id}).content.decode('utf-8')))
     17 #Total_Reply_by_Agent_df = pd.read_csv(StringIO(requests.get(f'https://altayyargroup.lightning.force.com/{Total_Reply_by_Agent}?isdtp=p1&export=1&enc=UTF-8&xf=csv', headers=sf.headers, cookies={'sid': sf.session_id}).content.decode('utf-8')))

~\anaconda3\lib\site-packages\requests\api.py in get(url, params, **kwargs)
     73     """
     74 
---> 75     return request('get', url, params=params, **kwargs)
     76 
     77 

~\anaconda3\lib\site-packages\requests\api.py in request(method, url, **kwargs)
     59     # cases, and look like a memory leak in others.
     60     with sessions.Session() as session:
---> 61         return session.request(method=method, url=url, **kwargs)
     62 
     63 

~\anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    540         }
    541         send_kwargs.update(settings)
--> 542         resp = self.send(prep, **send_kwargs)
    543 
    544         return resp

~\anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
    675             # Redirect resolving generator.
    676             gen = self.resolve_redirects(r, request, **kwargs)
--> 677             history = [resp for resp in gen]
    678         else:
    679             history = []

~\anaconda3\lib\site-packages\requests\sessions.py in <listcomp>(.0)
    675             # Redirect resolving generator.
    676             gen = self.resolve_redirects(r, request, **kwargs)
--> 677             history = [resp for resp in gen]
    678         else:
    679             history = []

~\anaconda3\lib\site-packages\requests\sessions.py in resolve_redirects(self, resp, req, stream, timeout, verify, cert, proxies, yield_requests, **adapter_kwargs)
    164 
    165             if len(resp.history) >= self.max_redirects:
--> 166                 raise TooManyRedirects('Exceeded {} redirects.'.format(self.max_redirects), response=resp)
    167 
    168             # Release the connection back into the pool.

TooManyRedirects: Exceeded 30 redirects.
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Mahmoud Badr
  • 303
  • 1
  • 11
  • 1
    From what I could gather. The requested page redirects to another for a SSO authentication. It's unclear how do you get the session_id but consider that they expire after a certain time. – f-starace Oct 16 '22 at 12:43
  • So do you recommend any solution as the code was working with me normally from a few days and i don't understand why this happens now – Mahmoud Badr Oct 16 '22 at 12:52
  • 1
    The problem is likely to refer to the login authentication and fetching of the correct session_id. My suggestion is to learn about [request.session](https://requests.readthedocs.io/en/latest/user/advanced/) and play with the network requests in order to tailor the correct request to get a valid session_id. Unfortunately authentication systems are often sites-specific and I can't possibly provide a one size fits all solution, but if you managed to log in the past, you should be able to fix the auth req in order to get a valid session_id cookie – f-starace Oct 16 '22 at 13:23
  • Also. I suggest you to divide your code in functions or at least to separate the data fetching side from the data parsing using requests. It will make it much easier to read your code and interpret error. It's not the best thing in the word if in a single line of code you may encounter an error from `requests`, `StringIO` and `pandas` – f-starace Oct 16 '22 at 13:30

2 Answers2

4

With enhanced domains the domain name has changed Please change the domain name from https://altayyargroup.lightning.force.com

to

https://altayyargroup.my.salesforce.com

1

Double check what the url should be now, login manually with browser and compare.

I suspect your admin didn't notify you about "enhanced domains" release update getting enabled in many organisations this weekend. Admins had lots of time to prepare, do trial runs (in sandbox and prod) and even option to postpone enabling it till January-February.

Either way what you're doing (pretending to be a browser to download some reports) is fragile, unsupported, not a real integration. You should plan a rewrite to something that uses proper SF analytics api.

eyescream
  • 18,088
  • 2
  • 34
  • 46