Fix save token (using pickle)

This commit is contained in:
Diego Pereyra 2022-08-08 21:59:27 -03:00
parent 470f05150d
commit 2ab14d9cd3

View file

@ -29,6 +29,7 @@ import core.decorators
import datetime import datetime
import os.path import os.path
import pickle
from google.auth.transport.requests import Request from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials from google.oauth2.credentials import Credentials
@ -60,7 +61,8 @@ class Module(core.module.Module):
# created automatically when the authorization flow completes for the first # created automatically when the authorization flow completes for the first
# time. # time.
if os.path.exists(self.__token): if os.path.exists(self.__token):
creds = Credentials.from_authorized_user_file(self.__token, SCOPES) with open(self.__token, 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in. # If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid: if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token: if creds and creds.expired and creds.refresh_token:
@ -71,8 +73,8 @@ class Module(core.module.Module):
) )
creds = flow.run_local_server(port=0) creds = flow.run_local_server(port=0)
# Save the credentials for the next run # Save the credentials for the next run
with open(self.__token, "w") as token: with open(self.__token, 'wb') as token:
token.write(creds.to_json()) pickle.dump(creds, token)
try: try:
service = build("calendar", "v3", credentials=creds) service = build("calendar", "v3", credentials=creds)