1

I am creating a Python script which reads a spreadsheet and issues Rest requests to an external service. A token must be obtained to issue requests to the Rest service, so the script needs a username and password to obtain the oauth2 token.

What's the best or standard way to hide or not have visible information in the Python script?

Sreeram M
  • 140
  • 10
Carlos Gonzalez
  • 607
  • 1
  • 5
  • 13
  • Read it from an external file. – Jan Christoph Terasa Jan 06 '21 at 13:23
  • 1
    Does this answer your question? [I need to securely store a username and password in Python, what are my options?](https://stackoverflow.com/questions/7014953/i-need-to-securely-store-a-username-and-password-in-python-what-are-my-options) – ayorgo Jan 06 '21 at 13:34

2 Answers2

1

I recommend using a config file. Let's create a config file and name it config.cfg. The file structure should look more or less like this:

[whatever]
key=qwerertyertywert2345
secret=sadfgwertgrtujdfgh

Then in python you can load it this way:

from configparser import ConfigParser

config = ConfigParser()
config.read('config.cfg')

my_key = config['whatever']['key']
my_secret = config['whatever']['secret']
Peeraponw
  • 101
  • 4
0

In general, the most standard way to handle secrets in Python is by putting them in runtime configuration.

You can do that by reading explicitly from external files or using os.getenv to read from environment variables.

Another way is to use a tool like python-decouple, which lets you use the environment (variables), a .env file, and an .ini file in a cascade, so that developers and operations can control the environment in local, dev, and production systems.

kojiro
  • 74,557
  • 19
  • 143
  • 201