Regenerating Google's ClientLogin AuthToken in Django
If your application makes use of Google API, you will most likely have to deal with AuthToken expiry (unless you use oAuth). As far as I know, AuthToken generated with ClientLogin authorization expires after 2 weeks. Therefore, it’s good to have a solution to auto regenerate it. First of all, create a model that stores the token and the last updated day.
fromdjango.dbimportmodelsfromdatetimeimportdatetime,timedeltaclassAuthToken(models.Model):token=models.TextField()last_updated=models.DateTimeField(auto_now_add=True)deftoken_update(self):# Check if the token's lifespan is more than n days (n=1 in this case)# You can adjust n value to whatever you wantifdatetime.now()-self.last_updated>timedelta(days=1):returnTruereturnFalse
Now let’s write a function that regenerates AuthToken and saves it back to database.