diff options
| author | 2019-12-11 13:49:21 -0800 | |
|---|---|---|
| committer | 2019-12-11 13:49:21 -0800 | |
| commit | 249a4c185ca9680258eb7a753307fbe8d0089b4b (patch) | |
| tree | daa596cafb3e2a80af9a668d3984ecd705d47cd3 | |
| parent | Reddit: use qualified_name attribute when removing the cog (diff) | |
Reddit: use expires_in from the response to calculate token expiration
| -rw-r--r-- | bot/cogs/reddit.py | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/bot/cogs/reddit.py b/bot/cogs/reddit.py index 111f3b8ab..083f90573 100644 --- a/bot/cogs/reddit.py +++ b/bot/cogs/reddit.py @@ -56,7 +56,7 @@ class Reddit(Cog): return self.bot.get_channel(Channels.reddit) async def get_access_token(self) -> None: - """Get Reddit access tokens.""" + """Get a Reddit API OAuth2 access token.""" headers = {"User-Agent": self.USER_AGENT} data = { "grant_type": "client_credentials", @@ -72,10 +72,11 @@ class Reddit(Cog): ) if response.status == 200 and response.content_type == "application/json": content = await response.json() + expiration = int(content["expires_in"]) - 60 # Subtract 1 minute for leeway. AccessToken = namedtuple("AccessToken", ["token", "expires_at"]) self.access_token = AccessToken( token=content["access_token"], - expires_at=datetime.utcnow() + timedelta(hours=1) + expires_at=datetime.utcnow() + timedelta(seconds=expiration) ) self.headers = { "Authorization": "bearer " + self.access_token.token, |