From cdf99641ba453369b008ea0750553f4935b5db03 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 28 Feb 2022 20:09:19 +0000 Subject: [PATCH] Implement getting TrueLayer access tokens --- handler/revolut.py | 6 +++--- handler/sinks/truelayer.py | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/handler/revolut.py b/handler/revolut.py index bd901d5..436b76d 100644 --- a/handler/revolut.py +++ b/handler/revolut.py @@ -79,9 +79,9 @@ class Revolut(object): settings.Revolut.RefreshToken = parsed["refresh_token"] settings.Revolut.SetupToken = "0" settings.write() - self.log.info("Refreshed refresh token") + self.log.info("Refreshed refresh token - Revolut") self.token = parsed["access_token"] - self.log.info("Refreshed access token") + self.log.info("Refreshed access token - Revolut") except KeyError: self.log.error(f"Token authorization didn't contain refresh or access token: {parsed}", parsed=parsed) return False @@ -115,7 +115,7 @@ class Revolut(object): if r.status_code == 200: if "access_token" in parsed.keys(): self.token = parsed["access_token"] - self.log.info("Refreshed access token") + self.log.info("Refreshed access token - Revolut") return True else: self.log.error(f"Token refresh didn't contain access token: {parsed}", parsed=parsed) diff --git a/handler/sinks/truelayer.py b/handler/sinks/truelayer.py index f914f6e..454cfc0 100644 --- a/handler/sinks/truelayer.py +++ b/handler/sinks/truelayer.py @@ -47,9 +47,9 @@ class TrueLayer(object): settings.TrueLayer.Monzo_RefreshToken = parsed["refresh_token"] settings.TrueLayer.Monzo_SetupToken = "0" settings.write() - self.log.info("Refreshed refresh token") + self.log.info("Refreshed refresh token - TrueLayer") self.token = parsed["access_token"] - self.log.info("Refreshed access token") + self.log.info("Refreshed access token - TrueLayer") except KeyError: self.log.error(f"Token authorization didn't contain refresh or access token: {parsed}", parsed=parsed) return False @@ -57,12 +57,39 @@ class TrueLayer(object): self.log.error(f"Cannot refresh token: {parsed}", parsed=parsed) return False - def get_new_token(self, _=None): + def get_new_token(self, fail=False): """ Exchange our refresh token for an access token. """ - print("calling get new token") - pass + headers = {"Content-Type": "application/x-www-form-urlencoded"} + data = { + "grant_type": "refresh_token", + "refresh_token": settings.TrueLayer.Monzo_RefreshToken, + "client_id": settings.TrueLayer.ID, + "client_secret": settings.TrueLayer.Key, + } + r = requests.post(f"{settings.TrueLayer.Base}/connect/token", data=data, headers=headers) + try: + parsed = r.json() + except JSONDecodeError: + if fail: + exit() + return False + if r.status_code == 200: + if "access_token" in parsed.keys(): + self.token = parsed["access_token"] + self.log.info("Refreshed access token - TrueLayer") + return True + else: + self.log.error(f"Token refresh didn't contain access token: {parsed}", parsed=parsed) + if fail: + exit() + return False + else: + self.log.error(f"Cannot refresh token: {parsed}", parsed=parsed) + if fail: + exit() + return False def get_institutions(self, filter_name=None): """