0

I am trying to get a report I created on SalesForce via simple_salesforce library in python. I am able to connect successfully. However, I get invalid session id error, because the link is wrong, that is created by simple_salesforce. The url I am trying to get is different from what simple_salesforce is searching (which is given in the error below).

The link I am trying to get is : "https://gkg-mfsa.lightning.force.com/lightning/r/Report/00O9N000000JwK2UAK/view?queryScope=userFolders"

But the link simple_salesforce is searching is : "https://gkg-mfsa.my.salesforce.com/services/data/v42.0/lightning/r/Report/00O9N000000JwK2UAK/view?queryScope=userFolders" (as given in the error)

How can I get simple_salesforce library to search for the link I am trying to get instead of what it looks for.

from simple_salesforce import Salesforce


sf = Salesforce(username='myUserName', 
                password='myPassword',
                security_token='mySecurityToken',
               instance_url = "")

report_id = 'myreportId'

sf.restful("lightning/r/Report/ + reportId + /view?queryScope=userFolders")

output

SalesforceExpiredSession: Expired session for https://gkg-mfsa.my.salesforce.com/services/data/v42.0/lightning/r/Report/00O9N000000JwK2UAK/view?queryScope=userFolders. Response content: [{'message': 'This session is not valid for use with the REST API', 'errorCode': 'INVALID_SESSION_ID'}]


sara
  • 31
  • 5
  • What happens when you make the report URL start wit "/"? – eyescream Dec 24 '22 at 10:31
  • @eyescream Unfortunately I get the same error. Instead of sf.restful("lightning/r/Report/ + reportId + /view?queryScope=userFolders"), I tried sf.restful("/lightning/r/Report/ + reportId + /view?queryScope=userFolders") – sara Dec 26 '22 at 06:06

1 Answers1

1

Grab the session id and base endpoint from successful login call

session_id, instance = SalesforceLogin(
    username='myemail@example.com',
    password='password',
    security_token='token')

And then run a REST request manually. But you'll have to pass the session id as a cookie, not as a "Authorisation Bearer <session_id>" http header.

There's an example in https://github.com/simple-salesforce/simple-salesforce/issues/584

And one of my old answers (showing raw http but still, should give you an idea) https://stackoverflow.com/a/56162619/313628, https://stackoverflow.com/a/57745683/313628

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • While this does work, it does not yield any text that can be seen on the browser. For example, I am trying to get a report but the output is only html. No text that is on the report is in the output. Do you have any suggestions how to get the text part? – sara Dec 26 '22 at 08:00
  • You want the CSV or Excel format? Experiment with url params, `?export=1&enc=UTF-8&cf=csv`, similar to what's in that "issue 584". They roughly corresponds to "values" behind buttons you click when doing the export manually so right click & inspect element will help you identify what is what. – eyescream Dec 26 '22 at 09:53
  • ?export=1&enc=UTF-8&cf=csv does not work for me, it yields the same output. I will ask another question for this issue – sara Dec 26 '22 at 11:01
  • I asked another question, https://stackoverflow.com/questions/74919748/get-text-body-from-salesforce-report – sara Dec 26 '22 at 11:26