Python Google Calendar API获取提醒

问题描述 投票:0回答:1

[嗨,我正试图从Google Calendar API获取特定日期范围内的提醒,即从今天到今天之后的两天。但是不幸的是,当我打电话给提醒者时,它给了我IndexError。这个你能帮我吗。提前致谢。 Iam附加以下代码,然后出现错误

from tkinter import *
import time
import datetime
from PIL import Image, ImageTk
import requests
import calendar
from apiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
import pickle

class Calendar(Frame):                 
    def __init__(self, parent):
        super(Calendar, self).__init__(bg='black')
        self.calendars()
        self.reminders()

    def calendars(self):    
        cal = calendar.month(2019,10)
        self.calendarlb = Label(self, text=cal, font="Helvetica 12", bg='black', fg='white')
        self.calendarlb.pack(side=TOP, anchor=N, fill=BOTH, expand=YES)

    def reminders(self):
        scopes = ['https://www.googleapis.com/auth/calendar']
        #flow = InstalledAppFlow.from_client_secrets_file("client_secret.json", scopes=scopes)
        #credentials = flow.run_console()
        #pickle.dump(credentials, open("token.pkl", "wb"))
        credentials = pickle.load(open("token.pkl", "rb"))
        service = build("calendar", "v3" , credentials=credentials)
        now = datetime.datetime.utcnow().isoformat() + 'Z'
        diff = datetime.timedelta(2)
        Maxtime = now + str(diff)
        result = service.calendarList().list().execute()
        calendar_id = result['items'][1]['id']
        result_new = service.events().list(calendarId=calendar_id, timeMin=now, timeMax=Maxtime).execute()
        result_newer = result_new['items'][0]['summary']
        result_newer1 = result_new['items'][1]['summary']
        result_newer2 = result_new['items'][2]['summary']
        result_newer3 = result_new['items'][3]['summary']
        #titlelb = Label(self, text="To-do List", font="Helvetica 30", bg='black', fg='white')
        #titlelb.pack(side=TOP, anchor=NW)
        result_newer1lb = Label(self, text=result_newer1, font="helvetica 15", bg='black', fg='white')
        result_newer1lb.pack(side=TOP,anchor=N)
        result_newer2lb = Label(self, text=result_newer2, font="helvetica 15", bg='black', fg='white')
        result_newer2lb.pack(side=TOP,anchor=N)
        result_newer3lb = Label(self, text=result_newer3, font="helvetica 15", bg='black', fg='white')
        result_newer3lb.pack(side=TOP,anchor=N)
        #self.after(1000,self.reminders)

错误:-

Traceback (most recent call last): 
File "[...]\Python36\SmartMirror\SmartMirror2.py", line 218, in w = FullscreenWindow() 
File "[...]\Python36\SmartMirror\SmartMirror2.py", line 204, in init self.calendar = Calendar(self.bottomFrame) 
File "[...]\Python36\SmartMirror\SmartMirror2.py", line 149, in init self.reminders() 
File "[...]\Python36\SmartMirror\SmartMirror2.py", line 170, in reminders result_newer = result_new['items'][0]['summary'] IndexError: list index out of range
python google-calendar-api google-api-client
1个回答
1
投票

关闭-阅读堆栈跟踪:

line 170, in reminders result_newer = result_new['items'][0]['summary'] 
IndexError: list index out of range

IndexError表示脚本正在尝试索引一个列表(result_new['items'][0]),但在这种情况下该列表为空。

重新运行脚本并在第170行之前检查result_new['items']的内容,以查看如何调整结果对象的解压缩方式。

© www.soinside.com 2019 - 2024. All rights reserved.