带有服务帐户的 GoogleCalendar API 中的凭据 (Java)
现在我需要实现一个应用程序来创建一些谷歌日历事件,但用于服务帐户。我在文档中看到了使用 AuthorizationCodeInstalledApp
(或类似)创建事件来创建 Credential
实例 (com.google.api.client.auth.oauth2.Credential) 的示例,但它适用于常规用户帐户,不适用于服务帐户(我已经拥有包含来自 ServiceAccount 的密钥信息的 JSON 文件)。 接下来的几行显示什么对我来说是理想的(当然不起作用,因为 Calendar.Builder 需要一些 Credential
obj 作为第三个参数)
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
GoogleCredentials credential = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
.createScoped(Collections.singleton(CalendarScopes.CALENDAR));
Calendar calendar = new Calendar.Builder(HTTP_TRANSPORT, jsonFactory, credential)
.setApplicationName(APPLICATION_NAME)
.build();
如何使用/获取 Credential
obj从 .JSON 密钥文件(服务帐户)与 Calendar.Builder
一起使用?或者也许我错过了其他东西和/或有另一种方法可以做到这一点?
谢谢!
Right now I need to implement an app to create some google-calendar events but for a service account. I saw examples in docs that create events using AuthorizationCodeInstalledApp
(or similar) to create a Credential
instance (com.google.api.client.auth.oauth2.Credential) but it works for regular user accounts and not for Service account (I already have the JSON file with key info from my ServiceAccount).
Next lines show what's the ideal for me (of course is not working because Calendar.Builder needs some Credential
obj as a third param)
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
GoogleCredentials credential = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
.createScoped(Collections.singleton(CalendarScopes.CALENDAR));
Calendar calendar = new Calendar.Builder(HTTP_TRANSPORT, jsonFactory, credential)
.setApplicationName(APPLICATION_NAME)
.build();
How I can get a Credential
obj using/from a .JSON key file (service account) to use with Calendar.Builder
? or maybe I'm missing something else and/or there is another way to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此示例应该可以帮助您入门,确保将 UserEmail 设置为您想要委派的工作区域中的用户的电子邮件地址。
其中之一应该有效。
版本 1:
版本 2:
这里是有关如何设置委派的链接。只需确保将范围替换为 google 日历范围即可。
This sample should get you started make sure to set the UserEmail to the email address of the user on your Workspace domain that you want to delegate to.
One of these should work.
version 1:
Version 2:
here is a link on how to set up delegation. Just make sure to swap out the scope for the google calendar scope.