带有服务帐户的 GoogleCalendar API 中的凭据 (Java)

发布于 2025-01-14 20:47:44 字数 968 浏览 3 评论 0原文

现在我需要实现一个应用程序来创建一些谷歌日历事件,但用于服务帐户。我在文档中看到了使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2025-01-21 20:47:44

此示例应该可以帮助您入门,确保将 UserEmail 设置为您想要委派的工作区域中的用户的电子邮件地址。

其中之一应该有效。

版本 1:

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = GoogleCredential
    .fromStream(new FileInputStream(KEY_FILE_LOCATION))
    .setServiceAccountUser(userEmail)
    .createScoped(CalendarScopes.all());

版本 2:

private GoogleCredential authorize1() {

    GoogleCredential credential = null;

    HttpTransport = GoogleNetHttpTransport.newTrustedTransport();

    JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    try {

      InputStream jsonFileStream =
          DriveSample.class.getClassLoader().getResourceAsStream("client_secrets.json");

      GoogleCredential readJsonFile = GoogleCredential
          .fromStream(jsonFileStream, httpTransport, JSON_FACTORY).createScoped(DriveScopes.all());

      credential = new GoogleCredential.Builder().setTransport(readJsonFile.getTransport())
          .setJsonFactory(readJsonFile.getJsonFactory())
          .setServiceAccountId(readJsonFile.getServiceAccountId())
          .setServiceAccountUser(userEmail)
          .setServiceAccountScopes(readJsonFile.getServiceAccountScopes())
          .setServiceAccountPrivateKey(readJsonFile.getServiceAccountPrivateKey()).build();
    } catch (IOException exception) {
      exception.printStackTrace();
    }
    return credential;
}

这里是有关如何设置委派的链接。只需确保将范围替换为 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:

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = GoogleCredential
    .fromStream(new FileInputStream(KEY_FILE_LOCATION))
    .setServiceAccountUser(userEmail)
    .createScoped(CalendarScopes.all());

Version 2:

private GoogleCredential authorize1() {

    GoogleCredential credential = null;

    HttpTransport = GoogleNetHttpTransport.newTrustedTransport();

    JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    try {

      InputStream jsonFileStream =
          DriveSample.class.getClassLoader().getResourceAsStream("client_secrets.json");

      GoogleCredential readJsonFile = GoogleCredential
          .fromStream(jsonFileStream, httpTransport, JSON_FACTORY).createScoped(DriveScopes.all());

      credential = new GoogleCredential.Builder().setTransport(readJsonFile.getTransport())
          .setJsonFactory(readJsonFile.getJsonFactory())
          .setServiceAccountId(readJsonFile.getServiceAccountId())
          .setServiceAccountUser(userEmail)
          .setServiceAccountScopes(readJsonFile.getServiceAccountScopes())
          .setServiceAccountPrivateKey(readJsonFile.getServiceAccountPrivateKey()).build();
    } catch (IOException exception) {
      exception.printStackTrace();
    }
    return credential;
}

here is a link on how to set up delegation. Just make sure to swap out the scope for the google calendar scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文