Files
tlw-database-tool/src/main/java/de/jeyp91/googlecalendar/GoogleCalendarConnector.java
T

52 lines
2.0 KiB
Java

package de.jeyp91.googlecalendar;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
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.gson.GsonFactory;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.Calendar;
import java.io.*;
import java.util.Collections;
import java.util.List;
public class GoogleCalendarConnector {
private static final String APPLICATION_NAME = "Google Calendar API Java Quickstart";
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final String SERVICE_CREDENTIALS_FILE_PATH = "/tippliga-4e0def9ef447.json";
/**
* Creates an authorized Credential object.
* @return An authorized Credential object.
* @throws IOException If the Google_Credentials.json file cannot be found.
*/
private static GoogleCredential getCredentials() throws IOException {
InputStream in = GoogleCalendarConnector.class.getResourceAsStream(SERVICE_CREDENTIALS_FILE_PATH);
return GoogleCredential.fromStream(in)
.createScoped(Collections.singleton(CalendarScopes.CALENDAR));
}
public static com.google.api.services.calendar.Calendar getSerivce() {
com.google.api.services.calendar.Calendar service = null;
final NetHttpTransport HTTP_TRANSPORT;
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials())
.setApplicationName(APPLICATION_NAME)
.build();
} catch (Exception e) {
e.printStackTrace();
}
return service;
}
}