API календаря Google не может получить цвет события - Python

1

У меня возникла проблема с использованием API календаря Google, я пытаюсь выяснить, какой цвет это событие, но по какой-то нечетной причине он не работает. Я попытался найти решение, но пока оно безнадежно.

 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'])
Теги:
arrays
google-calendar-api

1 ответ

0
Лучший ответ

self.event['colorId'] возвращает идентификатор цвета. Чтобы получить соответствующие значения цвета, вы должны вызвать метод 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

Надеюсь, поможет!! Не стесняйтесь спрашивать, есть ли у вас какие-то сомнения :)

Ещё вопросы

Сообщество Overcoder
Наверх
Меню