osantos
osantos

Reputation: 11

Credentials in GoogleCalendar API with service account (Java)

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!

Upvotes: 1

Views: 1141

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117281

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.

Upvotes: 1

Related Questions