|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | # [START calendar_quickstart] |
16 | | -""" |
17 | | -Shows basic usage of the Google Calendar API. Creates a Google Calendar API |
18 | | -service object and outputs a list of the next 10 events on the user's calendar. |
19 | | -""" |
20 | 16 | from __future__ import print_function |
21 | 17 | import datetime |
22 | 18 | from googleapiclient.discovery import build |
23 | 19 | from httplib2 import Http |
24 | 20 | from oauth2client import file, client, tools |
25 | 21 |
|
26 | | -# Setup the Calendar API |
| 22 | +# If modifying these scopes, delete the file token.json. |
27 | 23 | SCOPES = 'https://www.googleapis.com/auth/calendar.readonly' |
28 | | -store = file.Storage('token.json') |
29 | | -creds = store.get() |
30 | | -if not creds or creds.invalid: |
31 | | - flow = client.flow_from_clientsecrets('credentials.json', SCOPES) |
32 | | - creds = tools.run_flow(flow, store) |
33 | | -service = build('calendar', 'v3', http=creds.authorize(Http())) |
34 | 24 |
|
35 | | -# Call the Calendar API |
36 | | -now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time |
37 | | -print('Getting the upcoming 10 events') |
38 | | -events_result = service.events().list(calendarId='primary', timeMin=now, |
39 | | - maxResults=10, singleEvents=True, |
40 | | - orderBy='startTime').execute() |
41 | | -events = events_result.get('items', []) |
| 25 | +def main(): |
| 26 | + """Shows basic usage of the Google Calendar API. |
| 27 | + Prints the start and name of the next 10 events on the user's calendar. |
| 28 | + """ |
| 29 | + store = file.Storage('token.json') |
| 30 | + creds = store.get() |
| 31 | + if not creds or creds.invalid: |
| 32 | + flow = client.flow_from_clientsecrets('credentials.json', SCOPES) |
| 33 | + creds = tools.run_flow(flow, store) |
| 34 | + service = build('calendar', 'v3', http=creds.authorize(Http())) |
42 | 35 |
|
43 | | -if not events: |
44 | | - print('No upcoming events found.') |
45 | | -for event in events: |
46 | | - start = event['start'].get('dateTime', event['start'].get('date')) |
47 | | - print(start, event['summary']) |
| 36 | + # Call the Calendar API |
| 37 | + now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time |
| 38 | + print('Getting the upcoming 10 events') |
| 39 | + events_result = service.events().list(calendarId='primary', timeMin=now, |
| 40 | + maxResults=10, singleEvents=True, |
| 41 | + orderBy='startTime').execute() |
| 42 | + events = events_result.get('items', []) |
| 43 | + |
| 44 | + if not events: |
| 45 | + print('No upcoming events found.') |
| 46 | + for event in events: |
| 47 | + start = event['start'].get('dateTime', event['start'].get('date')) |
| 48 | + print(start, event['summary']) |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + main() |
48 | 52 | # [END calendar_quickstart] |
0 commit comments