lib 1400401 import com.google.api.client.googleapis.auth.oauth2.*; 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.http.HttpTransport; 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; private static final String APPLICATION_NAME = "BotCompany Calendar API Demo"; private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); static LS SCOPES = Collections.singletonList(CalendarScopes.CALENDAR_READONLY); static GoogleClientSecrets getClientSecrets() ctex { temp InputStream in = newFileInputStream(javaxSecretDir("google-botcompany-credentials.json")); ret GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); } /** * Creates an authorized Credential object. * @param HTTP_TRANSPORT The network HTTP Transport. * @return An authorized Credential object. * @throws IOException If the credentials.json file cannot be found. */ // UNUSED static Credential getCredentials(NetHttpTransport HTTP_TRANSPORT) throws IOException { GoogleClientSecrets clientSecrets = getClientSecrets(); // Build flow and trigger user authorization request. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) .setDataStoreFactory(new FileDataStoreFactory(mkdirs(programFile("tokens")))) .setAccessType("offline") .build(); LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); ret new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); } sS getAccessToken(S authorizationCode) ctex { GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport, new JacksonFactory, googleBotCompanyClientID(), googleBotCompanyClientSecret(), authorizationCode, "https://oauth2-login-demo.appspot.com/code") .execute(); S token = response.getAccessToken(); print("Access token: " + token); ret token; } static GoogleCredential createCredentialWithRefreshToken(HttpTransport transport, JsonFactory jsonFactory, GoogleClientSecrets clientSecrets, S accessToken, S refreshToken) { ret new GoogleCredential.Builder().setTransport(transport) .setJsonFactory(jsonFactory) .setClientSecrets(clientSecrets) .build() .setAccessToken(accessToken) .setRefreshToken(refreshToken); } // pass access token & refresh token as cmd line args! p { //getAccessToken(args[0]); GoogleClientSecrets clientSecrets = getClientSecrets(); NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = createCredentialWithRefreshToken(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, args[0], args[1]); // Build a new authorized API client service. //GoogleCredential credential = new GoogleCredential().setAccessToken(args[0]); Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) .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 items = events.getItems(); if (items.isEmpty()) { print("No upcoming events found."); } else { print("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); } } }