【问题标题】:Google Calendar API v3 batch update in pythonPython 中的 Google Calendar API v3 批量更新
【发布时间】:2015-02-01 18:49:27
【问题描述】:

我在使用新的 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)

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

【问题讨论】:

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


    【解决方案1】:

    有通用的 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)
    

    【讨论】:

    • 谢谢!看起来就是这样。
    • @JayLee 你能告诉我如何从批处理请求中呈现对 HTML 的整个响应....我正在处理 gmail api 并且我在 insert_event 函数中得到响应但我不是能够渲染它....
    • http 是什么?名称未定义。
    【解决方案2】:

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

    event = {
            'summary': 'Google I/O 2015',
            'description': 'A chance to hear more about google.',
            'start': {
                'dateTime': '2021-05-28T09:00:00-07:00',
                'timeZone': 'America/Los_Angeles',
            },
            'end': {
                'dateTime': '2021-05-28T17:00:00-07:00',
                'timeZone': 'America/Los_Angeles',
            },
    }
    
    event2 = {
            'summary': 'Rocky Balboa',
            'description': 'more products.',
            'start': {
                'dateTime': '2021-05-22T09:00:00-07:00',
                'timeZone': 'America/Los_Angeles',
            },
            'end': {
                'dateTime': '2021-05-22T17:00:00-07:00',
                'timeZone': 'America/Los_Angeles',
            },
    
    }
    
    from googleapiclient.http import BatchHttpRequest
    import httplib2
    from googleapiclient import discovery
    
    
    
    batch = service_calendar.new_batch_http_request()
    batch.add(service_calendar.events().insert(calendarId=calendar_id, body=event2))
    batch.add(service_calendar.events().insert(calendarId=calendar_id, body=event))
    batch.execute()
    

    【讨论】:

      【解决方案3】:

      这对我有用:

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-24
        • 1970-01-01
        • 2013-12-04
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        相关资源
        最近更新 更多