Nowadays, we can create a Python script to automate some processes by means of accessing the API services provided by other companies. To store those credential keys and sensitive information, using a .env file could be a solution, but you have to ensure you won’t share your .env to the cloud mistakenly.
The keyring library in Python is a safe password storage solution to avoid the former problem. This article will teach you how to use keyring to store your credential keys safely on your local device.
We have generated our Telegram API key in the previous article. Let’s try to store it in Python using the keyring library.
Step 1: Install keyring by
pip install keyring
Step 2: Import keyring in your Python script
import keyring
Step 3: store a credential key. For example,
keyring.set_password("api", "telegram", "your_user_id:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
Step 4: Try to print your saved credential key by
print(keyring.get_password("api", "telegram"))
Congratulation! You have saved your Telegram Bot API key into keyring. In the following article, we will create a Telegram Bot with simple authentication using Google Spreadsheet.
The complete Python script
import keyring
keyring.set_password("api", "telegram", "your_user_id:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
print(keyring.get_password("api", "telegram"))