如何为Google Cloud服务账号配置Google Calendar API作用域
作用域设置及错误修复方案
你返回的invalid_scope错误是因为当前配置的日历只读权限和你插入日历的写入操作权限不匹配,作用域设置分为两处:
- 代码侧配置
你代码中调用setServiceAccountScopes的位置就是服务账号作用域的配置位置,当前你使用的https://www.googleapis.com/auth/calendar.readonly为只读权限,需要替换为支持写入的作用域,修改后的凭证构建代码如下:
GoogleCredential credentials = new GoogleCredential.Builder() .setTransport(GoogleNetHttpTransport.newTrustedTransport()) .setJsonFactory(JacksonFactory.getDefaultInstance()) .setServiceAccountId("替换为你的服务账号ID") // 替换为日历全量读写权限,支持插入日历操作 .setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/calendar")) .setServiceAccountPrivateKeyFromP12File(new File("替换为你的p12密钥文件路径")) .build();
- 后台配置校验
你需要确认对应Google Cloud项目已经启用Google Calendar API,如果你用服务账号访问企业Workspace域内的用户日历,还需要在Workspace管理后台为该服务账号开启域权限委派,同时配置对应的日历作用域。
内容的提问来源于stack exchange,提问作者Pez
相关产品推荐
相关产品推荐

