2017-05-31 105 views
0

我正在使用Google Calendar API,併成功地將一個事件插入授權的主日曆中,但我想要硬編碼多個事件,這些事件在執行時,將填充用戶日曆。Google Calendar API:插入多個事件(使用Python)

例如:

  • 6月24日下午1:00 「買蘋果」
  • 6月26日下午3:00 「拿起衣服」
  • 8月21日下午1:00 「清潔汽車」
  • 9月1日下午2:00 「還書」

我的代碼現在的問題是:

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': 'Buy apples', 
    'start': {'dateTime': '2017-06-24T13:00:00%s' % GMT_OFF}, 
    'end': {'dateTime': '2017-06-24T14:00:00%s' % GMT_OFF}, 
} 

e = CAL.events().insert(calendarId='primary', 
         sendNotifications=True, body=EVENT).execute() 

print('''*** %r event added: 
    Start: %s 
    End: %s''' % (e['summary'].encode('utf-8'), 
        e['start']['dateTime'], e['end']['dateTime'])) 

我需要知道如何在同一時間加入剩餘的事件,作爲一個例子,日曆。一個Google日曆事件插入中的多個非週期性事件。

+0

傳遞事件的數組你是什麼如何將剩餘的事件添加到平均日曆在同一時間,我可以看到你可以添加一個事件,同樣你可以添加其他人,有什麼問題 –

+0

我需要添加更多的事件,但是如果我添加另一個事件,它只會添加第一個事件。如果我通過「EVENT =」添加另一個,它會添加上面提到的事件。如果我通過「EVENT2 =」添加另一個事件,它將忽略它。 –

回答

0

不幸的事件一次只能執行一個事件,這意味着events.insert()方法必須多次調用。要硬編碼幾個事件,您需要調用

e = CAL.events().insert(calendarId='primary', 
        sendNotifications=True, body=EVENT).execute() 

具有不同的「主體」參數集。

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 
EVENT1 = { 
    'summary': 'Buy apples', 
    'start': {'dateTime': '2017-06-24T13:00:00%s' % GMT_OFF}, 
    'end': {'dateTime': '2017-06-24T14:00:00%s' % GMT_OFF}, 
} 
EVENT2 = { 
    'summary': 'Buy Bannanas', 
    'start': {'dateTime': '2017-06-25T13:00:00%s' % GMT_OFF}, 
    'end': {'dateTime': '2017-06-25T14:00:00%s' % GMT_OFF}, 
} 
EVENT3 = { 
    'summary': 'Buy candy', 
    'start': {'dateTime': '2017-06-26T13:00:00%s' % GMT_OFF}, 
    'end': {'dateTime': '2017-06-26T14:00:00%s' % GMT_OFF}, 
} 

e = CAL.events().insert(calendarId='primary', 
       sendNotifications=True, body=EVENT1).execute() 
e = CAL.events().insert(calendarId='primary', 
       sendNotifications=True, body=EVENT2).execute() 
e = CAL.events().insert(calendarId='primary', 
       sendNotifications=True, body=EVENT3).execute() 

的谷歌日曆「身體」參數具體發生在一個單一的事件,將拋出一個400 HTTP錯誤與

+0

非常感謝!很有幫助。 :) –

+0

@ArjunGheewala如果這個答案是你正在尋找的那個,請將它標記爲接受的答案,將不勝感激 –