如何通过Google API读取公开共享日历?遇403权限不足问题求助
解决Google Calendar API 403权限不足问题
问题根源
你遇到的ACCESS_TOKEN_SCOPE_INSUFFICIENT错误核心原因是权限范围不匹配:
你当前使用的https://www.googleapis.com/auth/calendar.events仅授权访问日历事件,但代码中调用的calendars.get接口(用于获取日历基础详情)需要的是针对日历本身的权限范围,而非仅事件权限。
修复步骤
1. 调整权限范围(Scope)
将代码中的scope替换为以下任一有效选项:
- 只读权限(推荐,遵循最小权限原则):
https://www.googleapis.com/auth/calendar.readonly - 全权限(如需后续修改操作):
https://www.googleapis.com/auth/calendar
2. 确认日历共享设置
确保目标个人日历的共享设置为**"公开访问:查看所有详情"**:
打开Google日历,找到目标日历→点击右侧三个点→「设置和共享」→「访问权限」→勾选"公开访问"并选择"查看所有详情"
3. 修正后的代码
import { GoogleAPI } from "https://deno.land/x/google_deno_integration/mod.ts"; const serviceAccountConfig = await Deno.readTextFile(Deno.env.get('GOOGLE_APPLICATION_CREDENTIALS')); const credentials = JSON.parse(serviceAccountConfig); const cal_id = 'my-cal-id'; const api = new GoogleAPI({ email: "test-cal@gmadrid-natacion-f5fcd.iam.gserviceaccount.com", scope: ["https://www.googleapis.com/auth/calendar.readonly"], // 替换为正确的权限范围 key: credentials.private_key, }); const calget = await api.get(`https://www.googleapis.com/calendar/v3/calendars/${cal_id}`); console.log(calget);
额外说明
服务账号不需要配置"所有者"角色,仅需上述正确的API权限范围即可访问公开日历,建议调整角色遵循最小权限原则,避免过度授权。
内容的提问来源于stack exchange,提问作者Bertuz
相关产品推荐
相关产品推荐

