pythonpython-3.xsalesforcesimple-salesforce

SalesforceExpiredSession Error using simple_salesforce package in Python


I am trying to download a report I created on SalesForce using simple_salesforce package in python.

Below is the sample code:

from simple_salesforce import Salesforce
import requests
import pandas as pd
from io import StringIO


sf = Salesforce(username='myusername', 
                password='mypassword',
                security_token='mytoken',
                version='46.0')



report_id = 'myreportid'
sf.restful('analytics/reports/{}'.format(report_id))

However, this chunk of code yields the following error:

SalesforceExpiredSession: Expired session for https://company_name.my.salesforce.com/services/data/v46.0/analytics/reports/myreporid. Response content: [{'message': 'This session is not valid for use with the REST API', 'errorCode': 'INVALID_SESSION_ID'}]

Solution

  • (continuing from comments)

    My bad, typo. Does your Profile have "API Enabled" checkbox ticked?

    enter image description here

    And you said you can see success in login history, active session?

    What happens when you try to do same thing manually with workbench. Login, then in top menu Utilities -> REST Explorer should let you run your report.

    Maybe simple is creating a SOAP session id which for whatever reason is incompatible with REST API (to be fair I thought they were pretty interchangeable, maybe your company disabled REST API, I heard it's possible to request that from SF support...)

    If workbench works - you may have to login in simple different way, creating "connected app" and reading up about https://help.salesforce.com/s/articleView?id=remoteaccess_oauth_username_password_flow.htm&type=5&language=en_US for example