0

I am trying to get SalesForce report via python using requests library. I am able to login successfully. The output I get with python does not contain any text body. How can I extract the text from python output.


from simple_salesforce import Salesforce
import requests
import pandas as pd

sf = Salesforce(username='', 
                password='',
                security_token='')



export_url = 'https://gkg-mfsa.lightning.force.com/lightning/r/Report/00O9N000000JwK2UAK/?export=1&enc=UTF-8&cf=csv'

session = requests.Session()
response = session.get(export_url, 
                       headers=sf.headers, 
                       cookies={'sid': sf.session_id})
download_report = response.content.decode('utf-8')
print(download_report)

output

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">





<script>
function redirectOnLoad() {
if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://gkg-mfsa.my.salesforce.com?ec=302&startURL=%2Fvisualforce%2Fsession%3Furl%3Dhttps%253A%252F%252Fgkg-mfsa.lightning.force.com%252Flightning%252Fr%252FReport%252F00O9N000000JwK2UAK%252F%253Fexport%253D1%2526enc%253DUTF-8%2526cf%253Dcsv'); }  else 
if (window.location.replace){ 
window.location.replace('https://gkg-mfsa.my.salesforce.com?ec=302&startURL=%2Fvisualforce%2Fsession%3Furl%3Dhttps%253A%252F%252Fgkg-mfsa.lightning.force.com%252Flightning%252Fr%252FReport%252F00O9N000000JwK2UAK%252F%253Fexport%253D1%2526enc%253DUTF-8%2526cf%253Dcsv');
} else {
window.location.href ='https://gkg-mfsa.my.salesforce.com?ec=302&startURL=%2Fvisualforce%2Fsession%3Furl%3Dhttps%253A%252F%252Fgkg-mfsa.lightning.force.com%252Flightning%252Fr%252FReport%252F00O9N000000JwK2UAK%252F%253Fexport%253D1%2526enc%253DUTF-8%2526cf%253Dcsv';
} 
} 
redirectOnLoad();
</script>

</head>


</html>





<!-- Body events -->
<script type="text/javascript">function bodyOnLoad(){if(window.PreferenceBits){window.PreferenceBits.prototype.csrfToken="null";};}function bodyOnBeforeUnload(){}function bodyOnFocus(){}function bodyOnUnload(){}</script>
            
</body>
</html>


<!--
...................................................................................................
...................................................................................................
...................................................................................................
...................................................................................................
-->



How can I get the text part from the output?

Edit:

I have also tried simple_salesforce library approach, specifically using sf.restful to get the report. That gives me a invalid session id error, I have also posted a question about that, you can find it here

sara
  • 31
  • 5
  • Does this answer your question? [Export salesforce report as CSV](https://stackoverflow.com/questions/48443107/export-salesforce-report-as-csv) – hc_dev Dec 26 '22 at 11:29
  • Do the headers contain [`Accept: text/csv`](https://stackoverflow.com/questions/7076042/what-mime-type-should-i-use-for-csv) to [request the desired format](https://www.rfc-editor.org/rfc/rfc9110.html#name-accept) ? Can use `print(sf.headers)` for debugging. See also [related question](https://salesforce.stackexchange.com/questions/47414/download-a-report-using-python) or ask on [salesforce.se]. – hc_dev Dec 26 '22 at 11:35
  • @hc_dev I have checked that approach out, it did not help in my case, edited the question with more information on what I have tried – sara Dec 26 '22 at 11:39
  • @hc_dev print(sf.headers) returns {'Content-Type': 'application/json', 'Authorization': '', 'X-PrettyPrint': '1'}, I deleted the authorization part – sara Dec 26 '22 at 11:41

1 Answers1

1

This "works for me" (org doesn't have "enhanced domains" enabled yet), put your report id

from simple_salesforce import Salesforce
import requests

sf = Salesforce(username='secret@example.com', 
                password='hunter2',
                security_token='')

print(sf.sf_instance)
print(sf.session_id)

export_url = export_url = 'https://' + sf.sf_instance + '/' + '00O5J000000y5LVUAY?isdtp=p1&export=1&enc=UTF-8&xf=csv'

session = requests.Session()
response = session.get(export_url, 
                       headers=sf.headers, 
                       cookies={'sid': sf.session_id})
download_report = response.content.decode('utf-8')
print(download_report)
eyescream
  • 18,088
  • 2
  • 34
  • 46
  • This solution gives the same exact html output I added to the question – sara Dec 26 '22 at 12:28
  • does it print a plausible-looking session id (00D... exclamation mark, some random garbage later) and instance url that has no sight of anything "lightning" in the url. Do you see successful login in your user's login history in setup. Did you have to provide security token? or maybe you added your IP to trusted range in Setup -> Network Access? – eyescream Dec 26 '22 at 12:31
  • is it sandbox or production? The report url looks sandbox-ish... For sandboxes "simple" says to add `domain='test'` to your `Salesforce()` call. Did you login to prod but hope to pull report from sandbox? – eyescream Dec 26 '22 at 12:33
  • I get the session id in correct format with sf.session_id. Instance url is "'gkg-mfsa.my.salesforce.com". My login attempts seem active in login history. I did provide a security token. Trying domain = "test" approach – sara Dec 26 '22 at 13:09
  • I tried to set the domain as "test" but I got the error :SalesforceAuthenticationFailed: INVALID_LOGIN: Invalid username, password, security token; or user locked out. – sara Dec 26 '22 at 13:25
  • current domain is "login" – sara Dec 26 '22 at 13:25
  • go to setup in the org where the report is and try to search for "sandboxes" in the sidebar. if there's such option - you're in production and it should all work with the code I posted. What do you get if you manually go to the report's "export url" in browser, does it instant download the CSV? if you don't see "sandboxes" option in setup -> you are in a sandbox. need to adjust your login Salesforce() call, username probably with suffix and the domain = test (assuming you're allowed to login from test.salesforce.com, sometimes admins allow only custom Setup -> MyDomain) – eyescream Dec 26 '22 at 17:33
  • If I go to reports export url in browser, it instantly downloads the csv. I cannot see the sidebar where my report is in salesforce. But I inspected the report page and searched for sandbox, one result is found. 0 results came up when I searched for production. Does this mean, if I am in sandbox, I am not able to see the report? Unfortunately I could not even create a connected app (because I did not have permissions) and asked from a salesforce admin to do it. – sara Dec 27 '22 at 06:33
  • 1
    If your report is in sandbox you need `domain='test'` in the login call and correct username (most of the time they have sandbox suffix so "sara lastname at gkg com mfsa" or something like that). Go to the report org's Setup -> Company Information and look at org id (00D...). Compare it with beginning of session id you get back from successful login. Do they match? – eyescream Dec 27 '22 at 19:14
  • I asked salesforce admin and turns out I wasn't in sandbox :/ Organization id checks with session id – sara Dec 28 '22 at 06:49
  • I'm out of ideas then. This works with my production, with my developer edition (well, after changing the report id and credentials of course). You say login history entry looks good, manual navigation to URL insta downloads the CSV... Are you using same credentials for web and python login? Maybe something with permissions, really clutching at straws here – eyescream Dec 28 '22 at 07:20
  • 1
    Turns out the issue was that I did not have admin privileges, the code you provided works fine, unless the privileges are missing – sara Jan 03 '23 at 06:31