Skip to content

Instantly share code, notes, and snippets.

@hrendoh
Last active June 17, 2016 05:21
Show Gist options
  • Save hrendoh/17ac0a32f55b8458d6e52c7ace110340 to your computer and use it in GitHub Desktop.
Save hrendoh/17ac0a32f55b8458d6e52c7ace110340 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.util.Arrays;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.SecurityUtils;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.CalendarList;
import com.google.api.services.calendar.model.CalendarListEntry;
public class GoogleCalendarAPISampleJWT {
public static void main(String[] args) {
//System.setProperty("https.proxyHost", "127.0.0.1");
//System.setProperty("https.proxyPort", "8888");
try {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jacksonFactory = JacksonFactory.getDefaultInstance();
InputStream in = new FileInputStream(new File("resources/privatekey.p12"));
PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), in, "notasecret", "privatekey", "notasecret");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jacksonFactory)
.setServiceAccountId("google-calendar-sample@xxxxx-yyyyy-123456.iam.gserviceaccount.com")
.setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/calendar"))
.setServiceAccountPrivateKey(privateKey)
.setServiceAccountUser("foo@example.com")
.build();
Calendar service = new com.google.api.services.calendar.Calendar.Builder(
httpTransport, jacksonFactory, credential).setApplicationName("GoogleCalendarAPISampleJWT").build();
System.out.println("Your calendars:");
System.out.println();
CalendarList feed = service.calendarList().list().execute();
for (CalendarListEntry entry : feed.getItems()) {
System.out.println();
System.out.println("-----------------------------------------------");
System.out.println("ID: " + entry.getId());
System.out.println("Summary: " + entry.getSummary());
if (entry.getDescription() != null) {
System.out.println("Description: " + entry.getDescription());
}
}
} catch (GeneralSecurityException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println(e.getMessage());
}
System.exit(1);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.util.Arrays;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.SecurityUtils;
import com.google.api.services.calendar.Calendar;
public class GoogleCalendarAppSkeleton {
public static void main(String[] args) {
//System.setProperty("https.proxyHost", "127.0.0.1");
//System.setProperty("https.proxyPort", "8888");
try {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jacksonFactory = JacksonFactory.getDefaultInstance();
InputStream in = new FileInputStream(new File("resources/privatekey.p12"));
PrivateKey privateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), in, "notasecret", "privatekey", "notasecret");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jacksonFactory)
.setServiceAccountId("google-calendar-sample@xxxxx-yyyyy-123456.iam.gserviceaccount.com")
.setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/calendar"))
.setServiceAccountPrivateKey(privateKey)
.setServiceAccountUser("foo@example.com")
.build();
Calendar service = new com.google.api.services.calendar.Calendar.Builder(
httpTransport, jacksonFactory, credential).setApplicationName("GoogleCalendarAPISampleJWT").build();
// write your process
} catch (GeneralSecurityException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println(e.getMessage());
}
System.exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment