import os
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# If modifying these scopes, delete the file token.json.
SCOPES = ["https://www.googleapis.com/auth/tasks"]
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TOKEN_PATH = os.path.join(BASE_DIR, "token.json")
CLIENT_SECRET_PATH = os.path.join(BASE_DIR, "client_secret.json")
def get_service():
"""Shows basic usage of the Tasks API.
Prints the title of the first 10 task lists.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(TOKEN_PATH):
creds = Credentials.from_authorized_user_file(TOKEN_PATH, SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
if not os.path.exists(CLIENT_SECRET_PATH):
raise FileNotFoundError(f"client_secret.json not found at {CLIENT_SECRET_PATH}. Please verify it is in the project root.")
flow = InstalledAppFlow.from_client_secrets_file(
CLIENT_SECRET_PATH, SCOPES
)
# using port 0 to automatically select an open port
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(TOKEN_PATH, "w") as token:
token.write(creds.to_json())
service = build("tasks", "v1", credentials=creds)
return service
if __name__ == "__main__":
try:
service = get_service()
print("Authentication successful!")
except Exception as e:
print(f"Authentication failed: {e}")