function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Using the python bulk api in a sandbox

I feel like I'm doing something silly causing this problem, but here goes.

I am working on setting up a python workflow/api to use the bulk api with. I have been successful in our live account with reads. But using the sandbox keeps throwing errors about  <exceptionMessage>Invalid session id</exceptionMessage> 

Here's the code.
#!/usr/bin/env python
import os

from time import sleep
from simple_salesforce import Salesforce
from salesforce_bulk import SalesforceBulk


# * sandbox -- True if you want to login to ``, False
#                      if you want to login to ``.


def get_contacts(bulk):
    job = bulk.create_query_job("Lead", contentType='CSV')
    batch = bulk.query(job, "select Id,FirstName,LastName,Company from Lead")
    while not bulk.is_batch_done(job, batch):

    for row in bulk.get_batch_result_iter(job, batch, parse_csv=True):
        print row   #row is a dict

if "__main__" == __name__:
    sf = Salesforce(username=username, password=password,
                    security_token=security_token, sandbox=sandbox)
    bulk = SalesforceBulk(sessionId=sf.session_id, host=sf.sf_instance)

It authenticates and the sf object has a session/session_id. The bulk query fails with the stacktrace below. If I toggle sandbox to false and drop the sandbox name off the email address, I can run this code against production data and it works.

I'm pretty much diving right in here, so don't assume I know much about salesforce. Any feedback is appreciated. I kind of feel like there's something I'm missing with the sf_instance coming back from the login. But... I'm not sure what.
salesforce_bulk.salesforce_bulk.BulkApiError: [400] Bulk API HTTP Error result: <?xml version="1.0" encoding="UTF-8"?><error
 <exceptionMessage>Invalid session id</exceptionMessage>

2 things bit me here.

1. There was no data in the sandbox. I created some records.

2. The office IP was tied to a specific session. The code above worked fine on aws boxes.

The code above works if you have data present and your config allows access from where you are running the code at.