Google API Calendar calendar ID "primary" not working

143 Views Asked by At

I'm trying to integrate Google Calendar API and I've been able to configure the API using a Service Account with its ID and I've already shared my calendar with the new IAM email address that was assigned to me after I created the Service Account and downloaded the JSON file containing the secrets.

There is a list problem, anytime I want to list the event. Using primary keyword doesn't work unless I use my own email address and it lists out all the event on my calendar which I created.

This is the code snippet

  public void listEvent() {
    try {
      DateTime now = new DateTime(System.currentTimeMillis());
      Events events = calendar.events()
              .list("")
              .setCalendarId("primary")
              .setMaxResults(5)
              .setTimeMin(now)
              .setOrderBy("startTime")
              .setSingleEvents(true)
              .execute();
      List<Event> items = events.getItems();
      if (items.isEmpty()) {
        System.out.println("No upcoming events found.");
      } else {
        System.out.println("Upcoming events");
        for (Event event : items) {
    .................
           }
          System.out.printf("%s (%s)\n", event.getSummary(), start);
        }
      }
    } catch (IOException ignored) {

    }
  }

This is my configuration class too.


@Configuration
public class GoogleCalendarAdapter {

  private static final String APPLICATION_NAME = "Lam Health";
  private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
  private static final String TOKENS_DIRECTORY_PATH = "tokens";
  private static final List<String> SCOPES = List.of(CalendarScopes.CALENDAR_READONLY, CalendarScopes.CALENDAR_EVENTS);
  private static final String CREDENTIALS_FILE_PATH = "/secret/oauth-credential.json";
  private static final String SERVICE_CREDENTIALS_FILE_PATH = "/secret/service-credential.json";

  @Bean
  public Calendar getCalendar() throws GeneralSecurityException, IOException {
    final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    return new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpCredentialsAdapter(getServiceCredential()))
            .setApplicationName(APPLICATION_NAME)
            .build();
  }

  private static InputStream getCredentialInputStream(String path) throws IOException {
    InputStream in = GoogleCalendarAdapter.class.getResourceAsStream(path);
    if (in == null) {
      throw new FileNotFoundException("Resource not found: ".concat(path));
    }
    return in;
  }

  public GoogleCredentials getServiceCredential() throws IOException {
    GoogleCredentials credentials = GoogleCredentials.fromStream(
            Objects.requireNonNull(getCredentialInputStream(SERVICE_CREDENTIALS_FILE_PATH)))
            .createScoped(
                    List.of(CalendarScopes.CALENDAR, CalendarScopes.CALENDAR_EVENTS))
            .createDelegated("***@***.com");
    credentials.refreshIfExpired();
    return credentials;
  }
}
1

There are 1 best solutions below

2
Linda Lawton - DaImTo On

Inorder to use a service account with Google calendar you need to configure domain wide deligation to the service account from your google workspace domain.

once that is done the service account will be able to impersonate any user on the domain and primary will work.

To be clear sharing a calendar with a service account doesn't work anymore. You can't use service account with standard Gmail account calendars. it has to be done through workspace