■ 設定
ポイント
【1】 Gradle を使う(以下の「build.gradle」参照) 【2】「src/main/resources」に「client_secret.json」を置く
build.gradle
// Apply the java-library plugin to add support for Java Library apply plugin: 'java-library' apply plugin: 'application' repositories { mavenCentral() } dependencies { compile 'com.google.api-client:google-api-client:1.23.0' compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0' compile 'com.google.apis:google-api-services-calendar:v3-rev305-1.23.0' }
■ サンプル
CalendarQuickstart.java
import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.client.util.DateTime; import com.google.api.client.util.store.FileDataStoreFactory; import com.google.api.services.calendar.Calendar; import com.google.api.services.calendar.CalendarScopes; import com.google.api.services.calendar.model.Event; import com.google.api.services.calendar.model.Events; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; import java.security.GeneralSecurityException; import java.util.Collections; import java.util.List; public class CalendarQuickstart { private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart"; private static final JsonFactory JSON_FACTORY = JacksonFactory .getDefaultInstance(); private static final String CREDENTIALS_FOLDER = "credentials"; /** * Global instance of the scopes required by this quickstart. If modifying * these scopes, delete your previously saved credentials/ folder. */ private static final List<String> SCOPES = Collections .singletonList(CalendarScopes.CALENDAR_READONLY); private static final String CLIENT_SECRET_DIR = "client_secret.json"; /** * Creates an authorized Credential object. * * @param HTTP_TRANSPORT * The network HTTP Transport. * @return An authorized Credential object. * @throws IOException * If there is no client_secret. */ private static Credential getCredentials( final NetHttpTransport HTTP_TRANSPORT) throws IOException { // Load client secrets. URL url = ClassLoader.getSystemResource(CLIENT_SECRET_DIR); URI uri = URI.create(url.toString()); Path path = Paths.get(uri); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(new FileInputStream(path.toString()))); // Build flow and trigger user authorization request. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) .setDataStoreFactory( new FileDataStoreFactory(new java.io.File(CREDENTIALS_FOLDER))) .setAccessType("offline").build(); return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) .authorize("user"); } public static void main(String... args) throws IOException, GeneralSecurityException { // Build a new authorized API client service. final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport .newTrustedTransport(); Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)).setApplicationName(APPLICATION_NAME) .build(); // List the next 10 events from the primary calendar. DateTime now = new DateTime(System.currentTimeMillis()); Events events = service.events().list("primary").setMaxResults(10) .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) { DateTime start = event.getStart().getDateTime(); if (start == null) { start = event.getStart().getDate(); } System.out.printf("%s (%s)\n", event.getSummary(), start); } } } }
参考文献
http://kokonotsu.net/log/google-calendar-api-v3-%E3%81%9D%E3%81%AE%EF%BC%91-%E8%A8%AD%E5%AE%9A%E7%B7%A8/http://kokonotsu.net/log/google-calendar-api-v3-%E3%81%9D%E3%81%AE%EF%BC%92-%E8%AA%8D%E8%A8%BC%E7%B7%A8/
https://blog.hrendoh.com/google-calendar-api-client-java-with-service-account-credential/