Last active
June 17, 2016 05:21
-
-
Save hrendoh/17ac0a32f55b8458d6e52c7ace110340 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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