Google Calendar日历ID与iframe src编码互转及匹配问题
Google Calendar 嵌入iframe的编码规则与转换方法
一、编码核心规则
Google Calendar嵌入iframe的src中,指定日历的编码字符串是URL安全的Base64编码,基于原始日历ID处理,具体规则:
- 对原始日历ID(如
c_63orqu43gib0VN3mv75np2278o@group.calendar.google.com)做标准Base64编码 - 替换编码结果中的
+为-,/为_ - 移除末尾所有的
=填充字符
二、双向转换方法
1. 日历ID转嵌入用编码字符串
步骤:
- 将日历ID转为UTF-8编码的字节流
- 执行标准Base64编码
- 替换特殊字符适配URL安全要求
- 移除末尾的
=填充
示例代码(Python):
import base64 def calendar_id_to_embed_code(calendar_id): byte_data = calendar_id.encode('utf-8') b64_str = base64.b64encode(byte_data).decode('utf-8') return b64_str.replace('+', '-').replace('/', '_').rstrip('=')
2. 嵌入用编码字符串转日历ID
步骤:
- 补全末尾的
=,让字符串长度为4的倍数(Base64编码的分组要求) - 将
-还原为+,_还原为/,恢复标准Base64格式 - 解码为UTF-8字符串,得到原始日历ID
示例代码(Python):
import base64 def embed_code_to_calendar_id(embed_code): padding = 4 - len(embed_code) % 4 if padding != 4: embed_code += '=' * padding standard_b64 = embed_code.replace('-', '+').replace('_', '/') return base64.b64decode(standard_b64).decode('utf-8')
三、自动化生成iframe src的实用技巧
- 多日历展示时,可在每个编码字符串后追加
,{十六进制RGB颜色码}来指定日历显示颜色,比如编码字符串,#ff0000 - 完整iframe src模板示例:
https://calendar.google.com/calendar/embed?src={编码字符串}&ctz=Asia/Shanghai,还可追加mode=week等参数调整视图模式
内容的提问来源于stack exchange,提问作者Rex Schrader
相关产品推荐
相关产品推荐

