无法通过服务帐户访问 Google 日历 API
Cannot get access to Google Calendar API via Service Account
我正在尝试通过 Java 中的简单 POST 调用来检索我自己的 free/busy 日历,但我 运行 遇到了访问错误。基本上我得到一个 404,notFound 错误,如下所示:
{
"kind": "calendar#freeBusy",
"timeMin": "2019-05-28T13:00:00.000Z",
"timeMax": "2019-05-28T21:00:00.000Z",
"calendars": {
"me@mycompany.com": {
"errors": [
{
"domain": "global",
"reason": "notFound"
}
],
"busy": []
}
}
}
代码相当简单。如果我使用来自 OAuth Playground 的不记名令牌,我可以获得我需要的东西,一切正常。这是我的相关代码:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(Objects.requireNonNull(classLoader.getResource("...a59.json")).getFile());
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream(file))
.createScoped(CalendarScopes.all());
credential.refreshToken();
String accessToken = credential.getAccessToken();
我得到的 accessToken
值在上面的响应中产生。
对于我的服务帐户,我是在一个启用了日历 API 的项目中创建的。我还授予它 Project Owner
的角色,以便它可以完全访问项目中的设置。
我确信这对我的服务帐户配置来说是小而简单的事情,但我终究无法弄清楚它是什么。
您还需要一步。
您需要与您的服务帐户共享您的日历。
使用文本编辑器打开您在创建服务帐户时下载的 something-something.json
文件,您将找到所需的电子邮件地址作为 "client_email" key/value 的值对.
转到您的日历 "Settings and sharing" 并单击 "Share with specific people" 下方的“+ 添加人员”按钮。
您可能已经这样做了,但对于其他人,这里是使用服务帐户访问 Google 日历的完整过程:()
所以我设法自己解决了这个问题。似乎我的服务帐户设置正确,但我添加的权限却没有。作为记录,这就是我想要的:
- 获得 read-only 访问日历的权限。
- 能够检索我组织中任何用户的日历。
我已经创建了服务帐户,确保它具有全域委派授权,将日历和管理 API 添加到我拥有该服务帐户的项目中,但如果不添加我的服务帐户,我将无法使用它根据上面@Alex Baban 的消息添加到日历。
我回到了绘图板,因为将它添加到几十个帐户并不是一个理想的解决方案,然后我找到了它。我必须为我的服务帐户添加以下授权:
- https://www.googleapis.com/auth/admin.directory.resource.calendar
- https://www.googleapis.com/auth/calendar.events.readonly
- https://www.googleapis.com/auth/calendar.readonly
希望这对以后的其他人有所帮助。
我正在尝试通过 Java 中的简单 POST 调用来检索我自己的 free/busy 日历,但我 运行 遇到了访问错误。基本上我得到一个 404,notFound 错误,如下所示:
{
"kind": "calendar#freeBusy",
"timeMin": "2019-05-28T13:00:00.000Z",
"timeMax": "2019-05-28T21:00:00.000Z",
"calendars": {
"me@mycompany.com": {
"errors": [
{
"domain": "global",
"reason": "notFound"
}
],
"busy": []
}
}
}
代码相当简单。如果我使用来自 OAuth Playground 的不记名令牌,我可以获得我需要的东西,一切正常。这是我的相关代码:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(Objects.requireNonNull(classLoader.getResource("...a59.json")).getFile());
GoogleCredential credential = GoogleCredential
.fromStream(new FileInputStream(file))
.createScoped(CalendarScopes.all());
credential.refreshToken();
String accessToken = credential.getAccessToken();
我得到的 accessToken
值在上面的响应中产生。
对于我的服务帐户,我是在一个启用了日历 API 的项目中创建的。我还授予它 Project Owner
的角色,以便它可以完全访问项目中的设置。
我确信这对我的服务帐户配置来说是小而简单的事情,但我终究无法弄清楚它是什么。
您还需要一步。
您需要与您的服务帐户共享您的日历。
使用文本编辑器打开您在创建服务帐户时下载的 something-something.json
文件,您将找到所需的电子邮件地址作为 "client_email" key/value 的值对.
转到您的日历 "Settings and sharing" 并单击 "Share with specific people" 下方的“+ 添加人员”按钮。
您可能已经这样做了,但对于其他人,这里是使用服务帐户访问 Google 日历的完整过程:(
所以我设法自己解决了这个问题。似乎我的服务帐户设置正确,但我添加的权限却没有。作为记录,这就是我想要的:
- 获得 read-only 访问日历的权限。
- 能够检索我组织中任何用户的日历。
我已经创建了服务帐户,确保它具有全域委派授权,将日历和管理 API 添加到我拥有该服务帐户的项目中,但如果不添加我的服务帐户,我将无法使用它根据上面@Alex Baban 的消息添加到日历。
我回到了绘图板,因为将它添加到几十个帐户并不是一个理想的解决方案,然后我找到了它。我必须为我的服务帐户添加以下授权:
- https://www.googleapis.com/auth/admin.directory.resource.calendar
- https://www.googleapis.com/auth/calendar.events.readonly
- https://www.googleapis.com/auth/calendar.readonly
希望这对以后的其他人有所帮助。