Auto-generated transcript Alright, welcome back. No long intros on this one, let's just quickly recap what we did last time. We built this read git ignore function over here, and what this basically does is it's going to read the git ignore file of your project and basically just ignore all of the files inside this project so we don't upload them, right? That's what we did the last video. Now, this video, I want to basically build a feature that allows me to reconnect to the Google API without me having to manually connect every single time right so you know that um our tokens our access tokens are going to expire after a certain period of time right i want to refresh them automatically without me having to reconnect manually right so that's what we're going to build in this video so to do this let's go back to the main.go file over here because this is where we have all of the OAuth and authentication stuff right now over here. Let's create an enum as well. Let's say type API access status and I'll just call this an integer for now and We're going to create a bunch of statuses over here. So Let's say const. Let's say access expired and This will be a type of API access status and I will just call this Iota so that we can just auto assign all of the values over here right I'll create two more we'll say access granted and access rejected let's say and now that we have this I want to create a function to test the access to the API to see whether we can actually access it or not right so let's create a function call it test access we'll pass in the drive as well over here and this will just return an API access status right what we created up here. And the way I'm going to access the API or test the access to the API is I'm just going to run a very simple, very small API call over here. I'll just say drive API files list. And I will only list a single file from the API by using page size one. And I'll just call do over here. So this will be a very small API call that's just going to list one file out of our Google Drive and it'll be a fast call and we'll know whether we can access the API or not right now if there is an error over here hmm let's let's filter it out let's use strings dot contains and then say error dot error they should return a string over here and let's also convert it to lowercase just so we don't have any issues with matching the error right so let's say strings.toLower then you can say something like token expired and that should work now along with this one we also have to add a check for another similar error so let say strings strings error and this will be token has been expired so you might be wondering why do we need two of these right they both they both just mean the same thing well for some reason google's api returns both of these errors right in different scenarios i'm i don't know which exact scenario returns which error but both of these main mean the same thing and in both of these scenarios we're just gonna have to refresh the token right so i don't know why they return different error messages when they both mean the same thing but we need to handle both right so over here we're going to return access expired because both of these errors mean that the token is expired right otherwise if there's a different kind of error we'll just say access rejected right and otherwise if there is no error at all then we can just return access granted and that's all we need to do right and now to actually test this out right we can go over to the connect function the drive.connect function and we can run this function that we just built just say test access and pass in the drive pass in d that is the drive over here and if this status equals let's say access expired and we haven't tried to force refresh this already then we need to force refresh this right so basically if the status of this token is expired and we haven't tried to force refresh it already right that's when we need to force refresh it again right by force over here now we're gonna have to repeat all of this code over here so i just going to move it to a different function up here let's just create a function call it let's say create drive service and there we go and this will return the service over here let's pass in a bunch of parameters as well force refresh which will be a boolean and this will return a pointer to drive dot service there we go and then we can just call this over here by saying create drive service passing force refresh over here and there we go awesome so now what we can do is copy these two lines and paste them over here and if the status is expired and we haven't tried to force refresh it already then we should create the drive service and this time passing through over here to manually force refresh the entire thing right and then assign that to the drive.api over here we can also add a bunch of logs over here as well we can say something like i don know um log token has expired uh and a new line as well and down here we can say token has been rejected and down here we can say token is valid let's just say now let's actually test this out and before we do that let's go up here in the previous video in the drive dot connect function over here i I passed in force refresh true over here, right? So let's change this back to false because we want to reuse our tokens as much as we can, right? But if we need to force refresh, then we built the functionality to do that automatically. So let's test this out. This is just going to upload my folders from the config for now, right? So let's run this and see what happens. Okay, it says token is valid. So it's not going to refresh the token right now, all right? And as you can see, all of the uploading process works as well. just like we built in the last video. Okay, so now what I've done is I've basically just destroyed the entire token, malformed it over here just removing a bunch of characters from over here and this token is going to get rejected because it's just not valid anymore, right? I've just messed with it. And as you can see, I already ran the entire program and the token was rejected. I'll do it again over here and yeah, you can see token has been rejected and it's not going to be able to upload any further, right? Okay, so all of this works. Now there is one more thing I want to do over here and that is to actually be able to refresh the tokens. So let me explain. All of this code we wrote is going to work when the access and the refresh tokens both have expired, right? And so we need to go over to the URL on Google, right? And manually, you know, allow access to our Google Drive, right? But we still have a refresh token, right? And you can use that refresh token to refresh our access tokens. And the way to do that is up here in the get client function. So for now, you can see that we're just grabbing the token, and then just returning a client with it, right? What we can instead do is we can create something called a config dot token source, passing the context over here, which I guess for now is just going to be context dot background, also pass in the token over here. What the token source object does is it basically automatically refreshes the access token if it expires right so by using this we can you know make this whole program a lot better because it's just going to automatically refresh our access tokens as long as our refresh tokens are valid. So now that we've created this what we can do is say that that the token is ts.token and this will also return an error. So let's just quickly handle that as well. Let just say if error does not equal to nil log failed what was that to generate a token and print the error over here along with a new line and there we go and once we generated that new token let also save it again over here passing the token file path passing the token as well and instead of using config dot client let's use oauth2 dot new client instead and passing the token source over here so So now with all of these changes, we're going to be able to refresh our access tokens automatically, right? This token source object is going to automatically refresh our access tokens if they expire. And if it's still valid, if it hasn't expired yet, then it's just going to reuse it, right? So it's only going to refresh the token if it expires, all right? So this is super helpful for us. And that is all we're going to do for this video. We built the functionality to automatically refresh our tokens and everything so that we don't have to do it manually. You saw in the previous videos that I had to sometimes, you know, go manually and refresh the entire access token, allow access to my drive to the API again and again, right? I had to do that a couple times in the previous videos. We don't need to do that anymore, all right? Because with all of this code that we've written, it's just going to do all of that for us automatically, right? So now we can finally focus on the features instead of just constantly manually refreshing our tokens, right? and i've already did a quick test of the program as well as you can see it's going to first save the credential file can i zoom in over here let me see yeah it's going to save the credential file it's going to check whether the token is valid or not if it is valid then it's just going to continue if it is you know expired then it's going to allow us to refresh that token by giving us the the url to go authorize our accounts once again and if the token is expired oh sorry if the token has been rejected then it's just going to exit the program right because then it's not an expiry issue it's some other issue and we need to deal with that manually right and then it's just going to you know do all of the uploads and stuff as long as everything else is valid right so that's that for this video in the next one i'm going to build a cli so you can see over here uh if i go up all the way up to the main function you can see there's not really much interactivity in this program right like it's it's gonna upload all the folders right in the config but you can't really interact with the program in any way right the only the only interaction you have with this program is in the config file where you add a bunch of folders that you want to synchronize right what i want to do next is i want to create some sort of cli so that we can pass in a bunch of commands and stuff and just make this whole thing work for us right so that's something we're going to do in the next video stay tuned for that and thank you for watching this one thank you for following along with the project all this time and um yeah i'll see you in the next video