简体   繁体   中英

Google calendar API can't get event colorid - Python

I am having this issue using the google calendar API, I'm trying to get it to find out what color is the event but for some odd reason it's not working. I've tried to find a solution, but so far it has been hopeless.

 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'])

self.event['colorId'] returns the id of the color. To get the relevant color values, you have to call the colors().get() method.

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

Here's how to use it in your example:

.
.
.

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

Hope it helps!! Feel free to ask if you have any doubts :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM