Use a Config File to store User Credentials

This quick note shows a way of setting up a “config file” stored on your local machine to store user credentials for API calls. The example below shows how this could work for Salesforce credentials.

import pandas as pd
from simple_salesforce import Salesforce

Open Credentials

import os                                 # use os to navigate file structure
cwd = os.getcwd()                         # save the current working directory
os.chdir('/Users/ryanwingate/Documents/') # navigate to the directory where the config file is stored
import config                             # import the config file
sf = Salesforce(username=config.username,
                password=config.password,
                security_token=config.security_token)
os.chdir(cwd)                             # Restore the original working directory

Salesforce

desc = sf.describe()
objects = []
obj_labels = [obj['label']  for obj in desc['sobjects']]
obj_names  = [obj['name']   for obj in desc['sobjects']]
obj_custom = [obj['custom'] for obj in desc['sobjects']]
for label, name, custom in zip(obj_labels, obj_names, obj_custom):
    objects.append((label, name, custom))
objects = pd.DataFrame(objects,
                       columns = ['label','name','custom'])
print(str(objects.shape[0]) + ' objects')
objects.head(10);
464 objects

Example contents of config.py

username='[email protected]'
password=‘fake_password’
security_token=‘N2jrZEWeIMjLSfdwev032F9hQsRd’