public static void main(String[] args) { try { // initialize the transport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); // initialize the data store factory dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR); // authorization Credential credential = authorize(); // set up global Calendar instance client = new com.google.api.services.calendar.Calendar.Builder( httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build(); // run commands showCalendars(); addCalendarsUsingBatch(); Calendar calendar = addCalendar(); updateCalendar(calendar); addEvent(calendar); showEvents(calendar); deleteCalendarsUsingBatch(); deleteCalendar(calendar); } catch (IOException e) { System.err.println(e.getMessage()); } catch (Throwable t) { t.printStackTrace(); } System.exit(1); }
Credential credential = flow.createAndStoreCredential(response, "userID"); com.google.api.services.calendar.Calendar client = new com.google.api.services.calendar.Calendar.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build();
@Override public Calendar makeClient(String clientId, String clientSecret, Collection<String> scopes, String applicationName, String refreshToken, String accessToken, String emailAddress, String p12FileName, String user) { boolean serviceAccount = false; // if emailAddress and p12FileName values are present, assume Google Service Account if (null != emailAddress && !"".equals(emailAddress) && null != p12FileName && !"".equals(p12FileName)) { serviceAccount = true; } if (!serviceAccount && (clientId == null || clientSecret == null)) { throw new IllegalArgumentException("clientId and clientSecret are required to create Google Calendar client."); } try { Credential credential; if (serviceAccount) { credential = authorizeServiceAccount(emailAddress, p12FileName, scopes, user); } else { credential = authorize(clientId, clientSecret, scopes); if (refreshToken != null && !"".equals(refreshToken)) { credential.setRefreshToken(refreshToken); } if (accessToken != null && !"".equals(accessToken)) { credential.setAccessToken(accessToken); } } return new Calendar.Builder(transport, jsonFactory, credential).setApplicationName(applicationName).build(); } catch (Exception e) { throw new RuntimeCamelException("Could not create Google Calendar client.", e); } }
/** * Build and return an authorized google calendar client. * * @return an authorized calendar client service */ private com.google.api.services.calendar.Calendar getOrCreateGoogleCalendarClient() { String refreshToken = settingsService.getSettings().getCalendarSettings().getGoogleCalendarSettings().getRefreshToken(); if (googleCalendarClient != null && refreshToken != null && refreshTokenHashCode == refreshToken.hashCode()) { LOG.debug("use cached googleCalendarClient"); return googleCalendarClient; } try { LOG.info("create new googleCalendarClient"); if (refreshToken != null) { refreshTokenHashCode = refreshToken.hashCode(); } NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); TokenResponse tokenResponse = new TokenResponse(); tokenResponse.setRefreshToken(refreshToken); Credential credential = createCredentialWithRefreshToken(httpTransport, JSON_FACTORY, tokenResponse); return new com.google.api.services.calendar.Calendar.Builder( httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build(); } catch (GeneralSecurityException | IOException e) { LOG.error(e); } return null; }
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); } } } }
/** * Constructor for instantiating the Google Calendars data source. * @param settings the application settings * @throws Exception if an error occurs */ public GoogleCalendarSourceImpl(final Settings settings) throws Exception { if (settings.getUserSettings().needInternetProxy()) { Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(settings.getUserSettings().internetProxyHost(), settings .getUserSettings().internetProxyPort())); httpTransport = new NetHttpTransport.Builder().setProxy(proxy).build(); } else { httpTransport = new NetHttpTransport.Builder().build(); } obfuscateEmails = settings.getUserSettings().obfuscateAttendeeEmails(); syncOrganizerAndAttendees = settings.getUserSettings().googleSyncOrganizerAndAttendees(); LOG.info("Connecting to Google Calendar..."); final Credential credential = authorize(); client = new Calendar.Builder(httpTransport, jsonFactory, credential).setApplicationName(APPLICATION_NAME).build(); calendarId = getCalendarId(settings.getUserSettings().googleCalendarName()); calendarTimeZone = getCalendarTimeZone(calendarId); LOG.info("Connected to Google Calendar."); }
public static com.google.api.services.calendar.Calendar getCalendarService(GuildSettings g) throws Exception { if (g != null && g.useExternalCalendar()) { Credential credential = authorize(g); return new com.google.api.services.calendar.Calendar.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential).setApplicationName(APPLICATION_NAME).build(); } else { Credential credential = authorize(); return new com.google.api.services.calendar.Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build(); } } }
private synchronized Calendar makeCalendarInterface(TokensAndUrlAuthData authData) { Credential credential = credentialFactory.createCredential(authData); return new Calendar.Builder( credentialFactory.getHttpTransport(), credentialFactory.getJsonFactory(), credential) .setApplicationName(GoogleStaticObjects.APP_NAME) .build(); }
private synchronized Calendar makeCalendarInterface(TokensAndUrlAuthData authData) { Credential credential = credentialFactory.createCredential(authData); return new Calendar.Builder( credentialFactory.getHttpTransport(), credentialFactory.getJsonFactory(), credential) .setApplicationName(GoogleStaticObjects.APP_NAME) .build(); } }