Gmail Part 3 - Securing Your Passcode

If you have not read this blog post, I would recommend reading Gmail part 1 and part 2. In this blog post I will be covering how to secure your passcode in Python using the previous pieces of code I have made. The previous pieces of code would look like this:

An example image of what my previous script would look like.

An example image of what my previous script would look like.

So, your passcode would be stored out in the open which wouldn’t be very secure. To solve this issue, we are going to make this more secure through the usage of a JSON file. This is a file type that is primarily used for transmitting information which in our case would be suitable for our Python program.

1) DEVELOPING A JSON FILE

To start with, let’s make a JSON file of where we’re going to store the contents of the email and password. The file should look something like this:

An image example of what the JSON file should look like.

An image example of what the JSON file should look like.

Specify the email and password to be in quotes specifically. This is where we are going to store the email and password. So it should be something like:

{ 
    "email": "exampleemailhere@gmail.com",
    "password": "16characterpasswordhere"
}

So instead of your email and password being stored in the clear in the Python file, we’re going to store the email and 16 character password specifically in this file. Save the JSON file in the same directory as your Python file, so that the file knows what JSON file it is accessing. I’ve saved mine as “Json.JSON”. Now we’re going to edit the Python file.

2) Editing the python file

Okay, so we’ve added information to the JSON file. Now we’re going to have to change the Python file. I will be using the example code from my first blog post regarding this

An image of my altered code from my first blog post.

An image of my altered code from my first blog post.

2a) explaining the code

I have added the Json library to my Python file script. Next, I have told my program to open the Json file as a file. Next, I have stored everything that is collected from the file as a dictionary. So what will be stored in this dictionary is an email and a password, as this is how the JSON file was defined. Next, I have defined the variables “PERSONAL_EMAIL” and “PASSWORD” to access the information in the seperate file which is more secure compared to sensitive information being stored in plain sight in the python file. Next, I have edited the code so that I can test if the email will actually send to me and the JSON file method was successful.

3) receiving the email

If you have defined the JSON file successfully, run the program and you should receive a test email like so:

An image example of a test email I have received

An image example of a test email I have received

If the transmission is unsuccessful an error will be thrown. It’s important that the .py files that you have and the .JSON files are in the same directory, otherwise this will not work!

Note for the second script that the IMap Tools library works by reading from the first message ever in your inbox. Therefore if you have a lot of messages that are seen and not deleted, the program will be incredibly slow at generating new files for the messages that you have not already read.

To summarise, having a .JSON file is an easier and more secure way to store your information rather than storing the information directly in a variable. To learn more about .JSON files, be sure to read more here.

Author: Sophie Dillon