Title: Store API Credentials For Open Source Projects
Slug: store_api_credentials_for_open_source_projects
Summary: Store API Credentials For Open Source Projects
Date: 2016-11-01 12:00
Category: Python
Tags: Basic
Authors: Chris Albon
One issue which repeated comes up is how to manage private API credentials when the project is available on GitHub. This is the method I use for my own projects. I store all credentials in a JSON file and tell gitignore to not upload that file. Then when I am running that code locally, load the API credentials from the JSON file.
In [2]:
import json
In [10]:
credentials = {'access_secret': '392n39d93',
'access_token': 'sdf424f',
'consumer_key': 'sdf3223',
'consumer_secret': 'dsf2344'}
In [11]:
with open('credentials.json', 'w') as f:
json.dump(credentials, f, ensure_ascii=False)
Follow the instructions here.
Here is an example of a good gitignore file.
This step should be the one done inside your project or script.
In [12]:
# Import API Keys
with open('credentials.json') as creds:
credentials = json.load(creds)
In [13]:
credentials['consumer_key']
Out[13]: