这对我有用:
batch = service.new_batch_http_request()
for event_id in list_of_events:
batch.add(service.events().delete(eventId=event_id, calendarId=calendar_id), callback=callback)
batch.add(service.events().insert(body=api_format, calendarId=calendar_id))
batch.execute()
以下是创建服务的完整代码:
import os
import pickle
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from pathlib import Path
SCOPES = ['https://www.googleapis.com/auth/calendar.events',
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/spreadsheets.readonly']
def authorize(json_path='credentials_oauth2.json', token_path='token.pickle', scopes=SCOPES):
creds = None
# The file token.pickle 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):
with open(token_path, 'rb') as token:
creds = pickle.load(token)
# 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(
json_path, SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open(token_path, 'wb') as token:
pickle.dump(creds, token)
return creds
def get_calendar_service(json_path, token_path):
creds = authorize(json_path, token_path)
service = build('calendar', 'v3', credentials=creds, cache_discovery=False)
return service
JSON_PATH = Path(r"../credentials/credentials.json")
TOKEN_PATH = Path(r"../credentials/token.pickle")
service = get_calendar_service(JSON_PATH, TOKEN_PATH)