mirror of
https://github.com/vale981/outlook-oauth-hack
synced 2025-03-04 16:41:38 -05:00
saner storage
This commit is contained in:
parent
220d76c48f
commit
1d96d930db
4 changed files with 22 additions and 31 deletions
|
@ -18,10 +18,9 @@ def get_config(profile):
|
||||||
ClientId = config_data["ClientId"],
|
ClientId = config_data["ClientId"],
|
||||||
ClientSecret = config_data["ClientSecret"],
|
ClientSecret = config_data["ClientSecret"],
|
||||||
Scopes = config_data["Scopes"],
|
Scopes = config_data["Scopes"],
|
||||||
RefreshTokenFileName = cache_path / "imap_smtp_refresh_token",
|
CacheFile = cache_path / "cache.json",
|
||||||
AccessTokenFileName = cache_path / "imap_smtp_access_token",
|
|
||||||
Authority = config_data["Authority"] or None,
|
Authority = config_data["Authority"] or None,
|
||||||
Timeout = config_data.get("Timeout", 60 * 60))
|
)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if len(sys.argv) < 3:
|
if len(sys.argv) < 3:
|
||||||
|
|
|
@ -3,6 +3,5 @@ ClientId = "08162f7c-0fd2-4200-a84a-f25a4db0b584"
|
||||||
ClientSecret = "TxRBilcHdC6WGBee]fs?QR:SJ8nI[g82"
|
ClientSecret = "TxRBilcHdC6WGBee]fs?QR:SJ8nI[g82"
|
||||||
Scopes = ['https://outlook.office.com/IMAP.AccessAsUser.All','https://outlook.office.com/SMTP.Send']
|
Scopes = ['https://outlook.office.com/IMAP.AccessAsUser.All','https://outlook.office.com/SMTP.Send']
|
||||||
Authority = false
|
Authority = false
|
||||||
Timeout = 3600
|
|
||||||
|
|
||||||
[mcgill]
|
[mcgill]
|
||||||
|
|
12
get_token.py
12
get_token.py
|
@ -7,7 +7,7 @@ import sys
|
||||||
import threading
|
import threading
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import webbrowser
|
import webbrowser
|
||||||
|
import json
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
profile = sys.argv[1]
|
profile = sys.argv[1]
|
||||||
else:
|
else:
|
||||||
|
@ -78,10 +78,8 @@ if 'error' in token:
|
||||||
print(token)
|
print(token)
|
||||||
sys.exit("Failed to get access token")
|
sys.exit("Failed to get access token")
|
||||||
|
|
||||||
with open(profile_config.RefreshTokenFileName, 'w') as f:
|
|
||||||
print(f'Refresh token acquired, writing to file {profile_config.RefreshTokenFileName}')
|
|
||||||
f.write(token['refresh_token'])
|
|
||||||
|
|
||||||
with open(profile_config.AccessTokenFileName, 'w') as f:
|
with open(profile_config.CacheFile, 'w') as f:
|
||||||
print(f'Access token acquired, writing to file {profile_config.AccessTokenFileName}')
|
json.dump({'refresh_token': token['refresh_token'],
|
||||||
f.write(token['access_token'])
|
'expires_in': token['expires_in'],
|
||||||
|
'access_token': token['access_token']}, f)
|
||||||
|
|
|
@ -4,7 +4,7 @@ import config
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
import json
|
||||||
print_access_token = True
|
print_access_token = True
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
|
@ -18,34 +18,29 @@ profile_config = config.get_config(profile)
|
||||||
cache = SerializableTokenCache()
|
cache = SerializableTokenCache()
|
||||||
app = ConfidentialClientApplication(profile_config.ClientId, client_credential=profile_config.ClientSecret, token_cache=cache, authority=profile_config.Authority)
|
app = ConfidentialClientApplication(profile_config.ClientId, client_credential=profile_config.ClientSecret, token_cache=cache, authority=profile_config.Authority)
|
||||||
|
|
||||||
if profile_config.AccessTokenFileName.exists() and print_access_token:
|
|
||||||
st = os.stat(profile_config.AccessTokenFileName)
|
|
||||||
if (time.time()-st.st_mtime) < profile_config.Timeout:
|
|
||||||
with open(profile_config.AccessTokenFileName, 'r') as f:
|
|
||||||
print(f.read())
|
|
||||||
|
|
||||||
sys.exit(0)
|
if not profile_config.CacheFile.exists():
|
||||||
|
|
||||||
# check if file exists and error out if it doesn't
|
|
||||||
try:
|
|
||||||
old_refresh_token = open(profile_config.RefreshTokenFileName,'r').read()
|
|
||||||
|
|
||||||
except FileNotFoundError:
|
|
||||||
sys.exit("Please get the initial token by running `o365-get-token` first.")
|
sys.exit("Please get the initial token by running `o365-get-token` first.")
|
||||||
|
|
||||||
|
token_cache = json.loads(profile_config.CacheFile.read_text())
|
||||||
|
|
||||||
token = app.acquire_token_by_refresh_token(old_refresh_token,profile_config.Scopes)
|
st = os.stat(profile_config.CacheFile)
|
||||||
|
if (time.time()-st.st_mtime) < token_cache["expires_in"]:
|
||||||
|
print(token_cache["access_token"])
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
token = app.acquire_token_by_refresh_token(token_cache["refresh_token"],profile_config.Scopes)
|
||||||
|
|
||||||
if 'error' in token:
|
if 'error' in token:
|
||||||
print(token)
|
print(token)
|
||||||
sys.exit("Failed to get access token")
|
sys.exit("Failed to get access token")
|
||||||
|
|
||||||
# you're supposed to save the old refresh token each time
|
|
||||||
with open(profile_config.RefreshTokenFileName, 'w') as f:
|
|
||||||
#f.write(cache.find('RefreshToken')[0]['secret'])
|
|
||||||
f.write(token['refresh_token'])
|
|
||||||
|
|
||||||
with open(profile_config.AccessTokenFileName, 'w') as f:
|
# you're supposed to save the old refresh token each time
|
||||||
f.write(token['access_token'])
|
with open(profile_config.CacheFile, 'w') as f:
|
||||||
|
json.dump({'refresh_token': token['refresh_token'],
|
||||||
|
'expires_in': token['expires_in'],
|
||||||
|
'access_token': token['access_token']}, f)
|
||||||
if print_access_token:
|
if print_access_token:
|
||||||
print(token['access_token'])
|
print(token['access_token'])
|
||||||
|
|
Loading…
Add table
Reference in a new issue