Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

109
LINES

< > BotCompany Repo | #1028420 // Google Calendar Spike [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 4337K of libraries. Click here for Pure Java version (2895L/19K).

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<Event> 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);
      }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028420
Snippet name: Google Calendar Spike [OK]
Eternal ID of this version: #1028420/15
Text MD5: 00ae9eefc4e8abbf422e418347021b6c
Transpilation MD5: 8e1d069169b8b77369211f98c1523d11
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-26 18:27:13
Source code size: 4651 bytes / 109 lines
Pitched / IR pitched: No / No
Views / Downloads: 311 / 891
Version history: 14 change(s)
Referenced in: #1028554 - googleCalendarService