当前位置: 代码迷 >> python >> 通过API和域委托向用户添加Google日历不会设置颜色
  详细解决方案

通过API和域委托向用户添加Google日历不会设置颜色

热度:28   发布时间:2023-06-13 13:46:13.0

我正在使用python API向我的G Suite域用户推送日历。 我希望能够指定日历在用户的供稿中显示的颜色,但是日历总是显示出相同的蓝色。

这是添加日历的位置(这与日历出现在用户列表中一样,但忽略了颜色)

def google_add_cals_to_user(self, user):
    cals = GoogleCalendar.objects.calendars_for_user(user)
    cal_client = self.setup_user_cal(user.email)
    for cal in cals:
        cal_body = {
            'selected': True,
            'id': cal.address,
            'colorRgbFormat': True,
            'foregroundColor': '#000000',
            'backgroundColor': '#' + cal.colour
        }
        self.execute(cal_client.calendarList().insert(body=cal_body))

这是建立日历服务的地方:

def setup_user_cal(self, email):
    logger.debug('building calendar service for %s' % email)
    http_cal = self.key_auth(email, scopes=SCOPES_CAL)
    return self.build_service('calendar', 'v3', http_cal)

和验证码:

def key_auth(self, user_email, scopes=SCOPES, http=None):
    """
     This sets up auth using a private key set up as per: https://developers.google.com/drive/v2/web/delegation
    """
    f = file(settings.GOOGLE_SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
    key = f.read()
    f.close()
    credentials = SignedJwtAssertionCredentials(settings.GOOGLE_SERVICE_ACCOUNT_EMAIL, key,
                                                scope=scopes, sub=user_email)
    if not http:
        http = httplib2.Http()
    http = credentials.authorize(http)
    return http

和build_service:

def build_service(self, serviceName, version, http):
    retries = 10
    for n in range(1, retries+1):
        try:
            return build(serviceName, version, http=http)
        except AccessTokenRefreshError, e:
            ...

将日历ID和日历正文作为参数

在请求正文中,提供具有以下属性的 :

如果您查看日历资源的文档,将会看到

{
  "kind": "calendar#calendar",
  "etag": etag,
  "id": string,
  "summary": string,
  "description": string,
  "location": string,
  "timeZone": string
}

如您所见,这些是您唯一可以插入的字段。 颜色不在其中。 我也检查了 ,甚至无法更新日历颜色。

通常,我建议您为此添加功能请求。 但是,我看不到找到Google日历API的问题论坛。

  相关解决方案