Doleensk
Doleensk

Reputation: 19

Google calendar API - Appointment schedule

In Google calendar you can add "Appointment schedule" enter image description here

These are events that repeat periodically. But they are not created from ordinary events. They are created precisely by the schedule.

How can I access them using Google API?

I have tried service.events().list and service.events().instances and neither appear to return it.

I'm using python Here is my code using service.events().instances

import os
from dotenv import load_dotenv

import datetime
import time
import schedule

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
from googleapiclient.errors import HttpError

load_dotenv()

SCOPES = ["https://www.googleapis.com/auth/calendar"]


def main():
    creds = None
    if os.path.exists("token.json"):
        creds = Credentials.from_authorized_user_file(
            "token.json", 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:
            flow = InstalledAppFlow.from_client_secrets_file(
                "credentials.json", SCOPES
            )
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open("token.json", "w") as token:
            token.write(creds.to_json())

    try:
        service = build("calendar", "v3", credentials=creds)
        now = datetime.datetime.utcnow().isoformat() + 'Z'  
        week_later = (datetime.datetime.utcnow() +
                  datetime.timedelta(days=7)).isoformat() + 'Z'
        # Call the Calendar API
        instances_result = (
            service.events()
            .instances(
                calendarId="primary",
                timeMin=now,
                timeMax=week_later,
                maxResults=100,
                eventId='recurringEventId',
            )
            .execute()
        )
        instances = instances_result.get("items", [])
        if not instances:
            print("No upcoming instances found.")
            return

        # Prints the start and name of the next 10 events
        for instance in instances:
            start = instance["start"].get(
                "dateTime", instance["start"].get("date"))
            print(start, instance["summary"])
    

    except HttpError as error:
        print(f"An error occurred: {error}")


if __name__ == "__main__":
    main()

After running the program I get "No upcoming instances found."

Upvotes: 0

Views: 838

Answers (0)

Related Questions