谷歌日历API无法获取事件colorid Python

2024-09-27 23:22:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个问题使用谷歌日历API,我试图让它找出什么颜色是事件,但由于一些奇怪的原因,它不起作用。我试图找到一个解决办法,但到目前为止没有希望。在

 def greet(self):
    self.pikk=(self.variable.get())
    print(self.pikk)
    self.ckbx = self.ivar.get()
    print(self.ckbx)
    self.SCOPES = "https://www.googleapis.com/auth/calendar"
    self.store = file.Storage('credentials.json')
    self.creds = self.store.get()
    if not self.creds or self.creds.invalid:
        self.flow = client.flow_from_clientsecrets('client_secret.json', self.SCOPES)
        self.creds = tools.run_flow(self.flow, self.store)
    self.service = build('calendar', 'v3', http=self.creds.authorize(Http()))

    self.now = datetime.datetime.utcnow().isoformat() + 'Z'
    self.events_result = self.service.events().list(calendarId='primary', timeMin=self.now,
                                          maxResults=10, singleEvents=True,
                                          orderBy='startTime').execute()
    self.events = self.events_result.get('items', [])

    self.lievent = []

    if not self.events:
        print('No upcoming events found.')
    for self.event in self.events:
        self.start = self.event['start'].get('dateTime', self.event['start'].get('date'))
        print(self.start, self.event['colorId'])

Tags: storeselfeventjsongetifeventsflow
1条回答
网友
1楼 · 发布于 2024-09-27 23:22:27

self.event['colorId']返回颜色的id。要获得相关的颜色值,必须调用colors().get()方法。在

colors = service.colors().get().execute()

下面是如何在您的示例中使用它:

.
.
.

self.events_result = self.service.events().list(calendarId='primary', timeMin=self.now,
                                          maxResults=10, singleEvents=True,
                                          orderBy='startTime').execute()


# Get the list of colors
colors = service.colors().get().execute()

.
.
.

# Then when you print, you can use it like,

print(self.start, colors['calendar'][event['colorId']]['background'])

https://developers.google.com/calendar/v3/reference/colors/get

希望有帮助!!如果您有任何疑问,请随时询问:)

相关问题 更多 >

    热门问题