2017-06-01 73 views
0

非常感謝您花時間幫助!在插入事件之前插入新日曆Google API和刪除日曆事件(Python)

我創建一個Python腳本,我需要做到以下幾點:

  • 創建一個新的日曆;如果需要的話
稱它爲「XYZ」(而不是「主」日曆)
  • 插入硬編碼自定義事件
  • 不得不刪除整個「XYZ」日曆的功能和隨後插入事件

    我無法將新日曆插入到完整日曆中。 我已瀏覽Google日曆資源,但似乎無法遵循它。我的代碼現在如下:

    注:因爲我不知道如何插入一個新的日曆,事件正在編碼進入主日曆

    from __future__ import print_function 
    from apiclient.discovery import build 
    from httplib2 import Http 
    from oauth2client import file, client, tools 
    
    try: 
        import argparse 
        flags = argparse.ArgumentParser(parents 
        [tools.argparser]).parse_args() 
    except ImportError: 
        flags = None 
    
    SCOPES = 'https://www.googleapis.com/auth/calendar' 
    store = file.Storage('storage.json') 
    creds = store.get() 
    if not creds or creds.invalid: 
        flow = client.flow_from_clientsecrets('client_secret.json', SCOPES) 
        creds = tools.run_flow(flow, store, flags) \ 
          if flags else tools.run(flow, store) 
    
    CAL = build('calendar', 'v3', http=creds.authorize(Http())) 
    
    GMT_OFF = '-04:00'   # ET/MST/GMT-4 
    EVENT = { 
        'summary': 'Find venue', 
        'start': {'dateTime': '2017-06-23T13:00:00%s' % GMT_OFF}, 
        'end': {'dateTime': '2017-06-23T14:00:00%s' % GMT_OFF}, 
    } 
    EVENT2 = { 
        'summary': 'Visit museum', 
        'start': {'dateTime': '2017-07-09T13:00:00%s' % GMT_OFF}, 
        'end': {'dateTime': '2017-07-09T14:00:00%s' % GMT_OFF}, 
    } 
    
    EVENT3 = { 
        'summary': 'Buy fruits', 
        'start': {'dateTime': '2017-07-23T13:00:00%s' % GMT_OFF}, 
        'end': {'dateTime': '2017-07-23T14:00:00%s' % GMT_OFF}, 
    } 
    
    e = CAL.events().insert(calendarId='primary', 
            sendNotifications=True, body=EVENT).execute() 
    
    e = CAL.events().insert(calendarId='primary', 
            sendNotifications=True, body=EVENT2).execute() 
    
    e = CAL.events().insert(calendarId='primary', 
            sendNotifications=True, body=EVENT3).execute() 
    
    print('''*** %r event added: 
        Start: %s 
        End: %s''' % (e['summary'].encode('utf-8'), 
            e['start']['dateTime'], e['end']['dateTime'])) 
    

    所以,在Calendar事件插入,我想插入一個名爲'XYZ'的新日曆。然後,編碼的以下事件將被添加到'XYZ'。另外,我想添加能夠在用戶請求時從完整的Gmail中刪除日曆「XYZ」。

    再次感謝您的幫助!讓我知道,如果我能提供更清晰。

  • 回答

    0

    您需要添加使用calendars().insert() method的代碼。

    new_calendar = { 
        'summary': 'XYZ', 
        'timeZone': 'America/Los_Angeles' 
    } 
    
    created_calendar = CAL.calendars().insert(body=new_calendar).execute() 
    
    print created_calendar['id'] 
    

    然後,可以使用created_calendar的id將事件添加到新創建的日曆中。