Introduced the refresh token python script

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2023-01-26 19:26:53 +01:00
parent 38d8b519fb
commit 4021d4687d
3 changed files with 39 additions and 2 deletions

View File

@ -36,6 +36,7 @@ install:
$(INSTALL) -m 0755 $(srcdir)/gmail-imap-import.php $(DESTDIR)$(libexecdir)/piler $(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)/generate_stats.php $(DESTDIR)$(libexecdir)/piler
$(INSTALL) -m 0755 $(srcdir)/get-token.py $(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)/healthcheck.sh $(DESTDIR)$(libexecdir)/piler
$(INSTALL) -m 0755 $(srcdir)/mailstat.php $(DESTDIR)$(libexecdir)/piler $(INSTALL) -m 0755 $(srcdir)/mailstat.php $(DESTDIR)$(libexecdir)/piler
$(INSTALL) -m 0755 $(srcdir)/sign.php $(DESTDIR)$(libexecdir)/piler $(INSTALL) -m 0755 $(srcdir)/sign.php $(DESTDIR)$(libexecdir)/piler

View File

@ -16,6 +16,7 @@ ClientId = "c6843299-05c4-4c2e-9398-64dd42f14b6d" # Fix this value only
ClientSecret = "" ClientSecret = ""
Scopes = ['https://outlook.office.com/IMAP.AccessAsUser.All'] Scopes = ['https://outlook.office.com/IMAP.AccessAsUser.All']
AccessTokenFileName = "access_token" AccessTokenFileName = "access_token"
RefreshTokenFileName = "refresh_token"
cache = SerializableTokenCache() cache = SerializableTokenCache()
@ -70,7 +71,8 @@ if 'error' in token:
print(token) print(token)
sys.exit("Failed to get access token") sys.exit("Failed to get access token")
# print(token['refresh_token'])
with open(AccessTokenFileName, 'w') as f: with open(AccessTokenFileName, 'w') as f:
f.write(token['access_token']) f.write(token['access_token'])
with open(RefreshTokenFileName, 'w') as f:
f.write(token['refresh_token'])

34
util/refresh-token.py Executable file
View File

@ -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'])