[Python] Python Google Calendar API Batch Request

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    The following two lines of code are similar. The first line of code executes a single request. The second line of code adds a request to a batch, which is later executed.

    service.events().insert(calendarId='primary', body=eventBody).execute()
    batch.add(service.events().insert(calendarId='primary', body=eventBody))


    My issue is that the first line of code works perfectly and adds an event to my calendar, however when I run batch.execute() the API returns an error. So my eventBody is the same and so are the functions making the request to add an event. Nothing is different except for that I added the event to a batch and the batch will not execute.

    Is anybody familiar with batch requests able to guide me?

    #use this function to create a new appointment with json provided
    def newEvents():
    print("Caluclating")
    try:
    service = build("calendar", "v3", credentials=creds)
    # Call the Calendar API

    #callback is a parameter for batch request
    def callback(request_id, response, exception):
    if exception:
    print(f"Request {request_id} failed: {exception}")
    else:
    print(f"Request {request_id} succeeded: {response}")

    # Create a batch request
    batch = BatchHttpRequest(callback=callback)

    with open("C:\\Users\\weiss\\Desktop\\Programming\\Google API\\json_data.json", "r") as file:
    jsonDic = json.load(file)

    #go through the json objects and add them to the batch
    for i in range (len(jsonDic)):
    CalStartTime = datetime.datetime.strptime(jsonDic["start"]["dateTime"], "%m/%d/%Y %I:%M:%S %p").isoformat()
    CalEndTime = datetime.datetime.strptime(jsonDic["end"]["dateTime"], "%m/%d/%Y %I:%M:%S %p").isoformat()
    eventBody = {
    'summary': jsonDic['summary'],
    'location': jsonDic['location'],
    'description': jsonDic['description'],
    'start': {
    'dateTime': CalStartTime,
    'timeZone': jsonDic['start']['timeZone'],
    },
    'end': {
    'dateTime': CalEndTime,
    'timeZone': jsonDic['end']['timeZone'],
    },
    }
    pprint.pprint(eventBody)
    #service.events().insert(calendarId='primary', body=eventBody).execute()
    batch.add(service.events().insert(calendarId='primary', body=eventBody))

    batch.execute()

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

    Continue reading...

Compartilhe esta Página