56 lines
2.2 KiB
Java
56 lines
2.2 KiB
Java
package de.jeyp91.googlecalendar;
|
|
|
|
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.Calendar;
|
|
import com.google.api.services.calendar.CalendarScopes;
|
|
import com.google.auth.http.HttpCredentialsAdapter;
|
|
import com.google.auth.oauth2.GoogleCredentials;
|
|
|
|
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 GoogleCredentials getGoogleCredentials() throws IOException {
|
|
InputStream in = GoogleCalendarConnector.class.getResourceAsStream(SERVICE_CREDENTIALS_FILE_PATH);
|
|
GoogleCredentials credentials = GoogleCredentials.fromStream(in)
|
|
.createScoped(Collections.singleton(CalendarScopes.CALENDAR));
|
|
credentials.refreshIfExpired();
|
|
return credentials;
|
|
}
|
|
|
|
public static com.google.api.services.calendar.Calendar getSerivce() {
|
|
Calendar service = null;
|
|
|
|
final NetHttpTransport HTTP_TRANSPORT;
|
|
try {
|
|
// Build a new authorized API client service.
|
|
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
|
|
service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, null)
|
|
.setHttpRequestInitializer(new HttpCredentialsAdapter(getGoogleCredentials()))
|
|
.setApplicationName(APPLICATION_NAME)
|
|
.build();
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return service;
|
|
}
|
|
} |