From 4021d4687dd1bea6fe7f4ea3f19f43c8352ce016 Mon Sep 17 00:00:00 2001 From: Janos SUTO Date: Thu, 26 Jan 2023 19:26:53 +0100 Subject: [PATCH] Introduced the refresh token python script Signed-off-by: Janos SUTO --- util/Makefile.in | 1 + util/get-token.py | 6 ++++-- util/refresh-token.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100755 util/refresh-token.py diff --git a/util/Makefile.in b/util/Makefile.in index bfbb7ab7..622689ff 100644 --- a/util/Makefile.in +++ b/util/Makefile.in @@ -36,6 +36,7 @@ install: $(INSTALL) -m 0755 $(srcdir)/gmail-imap-import.php $(DESTDIR)$(libexecdir)/piler $(INSTALL) -m 0755 $(srcdir)/generate_stats.php $(DESTDIR)$(libexecdir)/piler $(INSTALL) -m 0755 $(srcdir)/get-token.py $(DESTDIR)$(libexecdir)/piler + $(INSTALL) -m 0755 $(srcdir)/refresh-token.py $(DESTDIR)$(libexecdir)/piler $(INSTALL) -m 0755 $(srcdir)/healthcheck.sh $(DESTDIR)$(libexecdir)/piler $(INSTALL) -m 0755 $(srcdir)/mailstat.php $(DESTDIR)$(libexecdir)/piler $(INSTALL) -m 0755 $(srcdir)/sign.php $(DESTDIR)$(libexecdir)/piler diff --git a/util/get-token.py b/util/get-token.py index 8927afd2..175088b3 100755 --- a/util/get-token.py +++ b/util/get-token.py @@ -16,6 +16,7 @@ ClientId = "c6843299-05c4-4c2e-9398-64dd42f14b6d" # Fix this value only ClientSecret = "" Scopes = ['https://outlook.office.com/IMAP.AccessAsUser.All'] AccessTokenFileName = "access_token" +RefreshTokenFileName = "refresh_token" cache = SerializableTokenCache() @@ -70,7 +71,8 @@ if 'error' in token: print(token) sys.exit("Failed to get access token") -# print(token['refresh_token']) - with open(AccessTokenFileName, 'w') as f: f.write(token['access_token']) + +with open(RefreshTokenFileName, 'w') as f: + f.write(token['refresh_token']) diff --git a/util/refresh-token.py b/util/refresh-token.py new file mode 100755 index 00000000..adee927d --- /dev/null +++ b/util/refresh-token.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 + +# Based on the https://github.com/UvA-FNWI/M365-IMAP project + +from msal import ConfidentialClientApplication, SerializableTokenCache +import sys + + +ClientId = "c6843299-05c4-4c2e-9398-64dd42f14b6d" # Fix this value only +ClientSecret = "" +Scopes = ['https://outlook.office.com/IMAP.AccessAsUser.All'] +AccessTokenFileName = "access_token" +RefreshTokenFileName = "refresh_token" + + +cache = SerializableTokenCache() +app = ConfidentialClientApplication(ClientId, + client_credential=ClientSecret, + token_cache=cache, authority=None) + +old_refresh_token = open(RefreshTokenFileName, 'r').read() + +token = app.acquire_token_by_refresh_token(old_refresh_token, Scopes) + + +if 'error' in token: + print(token) + sys.exit("Failed to get access token") + +with open(AccessTokenFileName, 'w') as f: + f.write(token['access_token']) + +with open(RefreshTokenFileName, 'w') as f: + f.write(token['refresh_token'])