WEBVTT

00:00.080 --> 00:03.470
In this lesson, we will focus on how to get the password.

00:03.470 --> 00:06.410
So the app password in the Python program.

00:06.410 --> 00:11.240
So what do we need to send an email to be able to actually log in and send the email.

00:11.240 --> 00:13.190
We need the email address.

00:13.190 --> 00:16.520
So for me that was the course Raspberry Pi email address.

00:16.520 --> 00:20.390
And we need the app password that we have just generated before now.

00:20.390 --> 00:27.050
The thing is you could do passwords is equal to and just write the app password here in your code.

00:27.050 --> 00:33.200
That would work, but as best practice it's better not to put any password in the code directly.

00:33.200 --> 00:37.880
So here we will do a very simple solution that works for simple applications.

00:37.910 --> 00:42.890
Okay, it's not really the most secure, but it's good enough for simple stuff that you do with your

00:42.890 --> 00:43.550
Raspberry Pi.

00:43.580 --> 00:49.490
So instead of writing the password here, we are going to write it inside a file somewhere, and then

00:49.520 --> 00:52.430
open the file and read the password from the file.

00:52.430 --> 00:54.980
So let's go to the terminal.

00:55.070 --> 00:56.660
You can open a new terminal.

00:56.990 --> 01:03.290
Let's clear that and you go to your home directory and where are we going to put the password.

01:03.290 --> 01:07.160
So for now if I do ls I have my temp file.

01:07.190 --> 01:08.840
So maybe you did the same as me.

01:08.870 --> 01:12.950
Or maybe you just copied the password in a different place.

01:12.950 --> 01:14.210
That's really not a problem.

01:14.240 --> 01:17.210
And in this I have my app password.

01:17.240 --> 01:24.260
Now if I do so I'm going to do ls Dash A to see all the folders and files, including the hidden ones.

01:24.260 --> 01:28.040
And well for example we have a dot local folder here.

01:28.040 --> 01:31.940
So I'm going to go inside this with CD dot local.

01:31.970 --> 01:36.140
I can press tab also and I have a share.

01:36.140 --> 01:39.860
So I'm going to go inside the share okay.

01:39.890 --> 01:42.260
Where I already have some stuff.

01:42.260 --> 01:51.710
And let's say I want to put my password here and I want to name the file dot email password as an example.

01:51.710 --> 01:56.180
So what you can do here, you can create the file with for example touch.

01:56.210 --> 01:58.820
You create the file, then you edit it.

01:58.820 --> 02:06.940
You can directly use nano to edit the file, but here what I will do is I will directly use this file

02:06.940 --> 02:08.290
that we have created here.

02:08.290 --> 02:11.080
So this temp file in our home directory.

02:11.080 --> 02:14.200
And I'm going to move it from there to here.

02:14.230 --> 02:15.010
How to do this.

02:15.040 --> 02:17.110
Well I have the MV command.

02:17.110 --> 02:24.160
And then because I'm already at the destination, I could go back to folders and then use mv temp and

02:24.160 --> 02:25.240
then put the destination.

02:25.240 --> 02:28.240
But I can also do this from the destination.

02:28.240 --> 02:34.150
In this case I will do dot dot dot dot slash temp and I use the Autocompletion.

02:34.180 --> 02:38.680
Okay, because I need to go back one folder, two folders back to the home directory.

02:38.680 --> 02:39.730
And here is the file.

02:39.730 --> 02:44.590
So I want to take this file and copy it right where I am.

02:44.920 --> 02:46.810
If I want to do this I can just do dot.

02:47.020 --> 02:49.090
Dot means the current folder.

02:49.090 --> 02:52.810
And what I can also do is I can rename the file at the same time.

02:52.810 --> 02:58.510
So I'm going to do dot email underscore password I press enter.

02:58.540 --> 03:00.520
Now I have my email password.

03:00.550 --> 03:01.720
Actually, it's a hidden file.

03:01.750 --> 03:03.820
So I need to do a.

03:04.480 --> 03:07.210
You see, I have my email password.

03:07.330 --> 03:08.830
Let's just verify.

03:10.750 --> 03:11.170
Okay.

03:11.170 --> 03:11.710
It's here.

03:11.710 --> 03:14.110
And if I go back to the home directory.

03:14.830 --> 03:17.020
So yeah it's not here anymore.

03:17.050 --> 03:17.350
Okay.

03:17.380 --> 03:23.890
So to recap you go to dot local share and then you create a dot email password file.

03:23.890 --> 03:28.030
And you put the password so the app password inside this.

03:28.030 --> 03:32.050
And then we're going to read this file from the Python script.

03:32.050 --> 03:38.020
So this method is what is better than hardcoding the password in the code for sure.

03:38.020 --> 03:41.860
But still your password is somewhere in the Raspberry Pi okay.

03:41.860 --> 03:43.120
It's not really protected.

03:43.120 --> 03:45.280
So it's fine for small projects, okay.

03:45.310 --> 03:47.950
Or if you are the only one to have access to the Raspberry Pi.

03:47.950 --> 03:52.660
But if you have, let's say if you have a Raspberry Pi in open environment that lots of people can get

03:52.690 --> 03:56.920
access to it, or if you want to do something a bit more professional, then you will need to use a

03:56.920 --> 04:02.310
different way to be able to kind of connect to your Google account to send an email.

04:02.310 --> 04:05.970
And you can use, for example, there's a protocol called OAuth two.

04:06.000 --> 04:11.820
Okay, this is much more complicated, but it is something if you want, you can explore after the course.

04:11.850 --> 04:13.440
For now, this solution is good.

04:13.440 --> 04:18.090
And I would say for simple projects where you know that it's only you who have access to the Raspberry

04:18.090 --> 04:20.940
Pi, nothing bad can happen in any way.

04:20.940 --> 04:23.550
You have used the new Google account.

04:23.580 --> 04:24.810
Okay, not your personal one.

04:24.810 --> 04:29.880
So you have used the new Google account that contains no document, no photo, no files, no email.

04:29.880 --> 04:34.260
So even if there is an issue where the worst case scenario, you don't really lose anything.

04:34.290 --> 04:36.060
All right, now let's go to the Python code.

04:36.060 --> 04:37.920
So I create my password.

04:37.950 --> 04:42.090
Still I'm going to create a password variable with an empty string.

04:42.300 --> 04:45.870
And then I will do with open okay.

04:45.870 --> 04:47.520
So we need to open the file.

04:48.150 --> 04:49.680
And the file is.

04:49.680 --> 05:03.210
So let's put the absolute path slash home slash pi slash dot local slash share slash dot email password.

05:03.570 --> 05:07.920
If you have a doubt, you can go back to the terminal and check that it's the correct path, and then

05:07.920 --> 05:13.200
I will open it with only read permission because I'm not going to write anything, so I just need to

05:13.230 --> 05:13.920
read from it.

05:13.920 --> 05:21.840
And then as f or as file and then I can do password is equal to f dot read.

05:22.230 --> 05:25.470
And that's it because the file only contains the password.

05:25.470 --> 05:28.800
So I put what I read from the file to the password variable.

05:28.800 --> 05:31.050
And note I created a password variable before.

05:31.080 --> 05:36.720
As you can see this is going to be a scope issue because here we are inside a more nested scope.

05:36.720 --> 05:44.310
So if I get the password here and then I try to use it somewhere here, then I might not be able to

05:44.340 --> 05:45.090
get access to it.

05:45.090 --> 05:47.430
So I first create it here.

05:47.430 --> 05:51.420
Then I can use it inside the scope and I can continue to use it afterwards.

05:51.450 --> 05:56.580
And just one thing is that, well, if you ever have.

05:56.580 --> 06:00.800
So you see that we should not have any space at the end.

06:00.830 --> 06:03.620
Here there is a newline character, I think.

06:03.620 --> 06:09.950
But in case you are in doubt, and in case you might have added a space at the end, then the password

06:09.950 --> 06:13.070
is going to contain a space which might produce an error later.

06:13.070 --> 06:18.560
So you can use dots or strip after f dot read.

06:18.590 --> 06:18.980
Okay.

06:19.010 --> 06:24.770
So if you do this on a string because this is going to be a string, if you do a strip on a string,

06:24.770 --> 06:27.560
it's going to remove any trailing character.

06:27.560 --> 06:29.660
And here is going to remove the empty characters.

06:29.660 --> 06:33.170
So the new line, the spaces, etc..

06:33.200 --> 06:33.680
All right.

06:33.680 --> 06:38.360
And with this we have the password that we can use in the code.

06:38.390 --> 06:40.640
I'm not going to print it here okay.

06:40.670 --> 06:42.530
Not going to print it with Python.

06:42.800 --> 06:45.680
And I encourage you not to print your passwords okay.

06:45.710 --> 06:51.710
When you run some code because well, if you have some logs that can be stored somewhere and you print

06:51.710 --> 06:54.230
the password, then you just give the password in the logs.

06:54.230 --> 06:57.560
And then whoever can read the log can also get your password.
