python中的Google Calendar API v3批量更新

时间:2014-12-04 01:28:34

标签: python google-calendar-api batching google-api-v3

我在使用新的Google Calendar API v3 python库时遇到问题。文档似乎有点稀疏。我可以在特定日历上进行身份验证并获取活动。但是,我想使用gdata库执行批量更新:

# example from gdata
# feed that holds all the batch rquest entries
  request_feed = gdata.calendar.data.CalendarEventFeed()
# add the update entries to the batch feed
  request_feed.AddUpdate(entry=updateEntry1)
  request_feed.AddUpdate(entry=updateEntry2)
# submit the batch request to the server
  response_feed = self.cal_client.ExecuteBatch(request_feed, gdata.calendar.client.DEFAULT_BATCH_URL)

html中有一个https://developers.google.com/google-apps/calendar/batch#example示例。但我可以使用python库吗?

3 个答案:

答案 0 :(得分:3)

有通用的Google API Python库批处理说明here。尝试类似:

from apiclient.http import BatchHttpRequest

def insert_event(request_id, response, exception):
  if exception is not None:
    # Do something with the exception
     pass
  else:
    # Do something with the response
    pass

service = build('calendar', 'v3')

batch = BatchHttpRequest(callback=insert_event)

batch.add(service.events().quickAdd(calendarId="you@domain.com",
  text="Lunch with Jim on Friday"))
batch.add(service.events().quickAdd(calendarId="you@domain.com",
  text="Dinner with Amy on Saturday"))
batch.add(service.events().quickAdd(calendarId="you@domain.com",
  text="Breakfast with John on Sunday"))
batch.execute(http=http)

答案 1 :(得分:0)

另一种方式,使用日历 API 中的 new_batch_http_request 方法:

Option Explicit

Function Race()
    Dim wn As Workbook, sh As Worksheet, rng As Range
    
    ActiveWorkbook.Worksheets(Array("GP", "F1")).Copy   ' into the automatically added WB
    Set wn = ActiveWorkbook ' new WB is active
    
    For Each sh In wn.Worksheets
        Set rng = sh.ListObjects(1).Range
        sh.ListObjects(1).Unlist
        With rng
            .Value = .Value
            .Interior.ColorIndex = xlColorIndexNone
            .Font.ColorIndex = xlColorIndexAutomatic
            .Borders.LineStyle = xlLineStyleNone
            Intersect(rng(1).EntireRow, rng).Borders.LineStyle = xlContinuous
        End With
        sh.Activate
        sh.Range("A1").Select
    Next sh
    
    wn.SaveAs "Q:\Racing\Results\" & Format(Date, "DDMMYY") & _
              " Grand prix & Formula1" & ".xlsx"
    wn.Close False
End Function

答案 2 :(得分:0)

这对我有用:

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)