Download Jar. Uses 4337K of libraries. Click here for Pure Java version (2895L/19K).
1 | lib 1400401 |
2 | |
3 | import com.google.api.client.googleapis.auth.oauth2.*; |
4 | |
5 | import com.google.api.client.auth.oauth2.Credential; |
6 | import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; |
7 | import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; |
8 | import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; |
9 | import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; |
10 | import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
11 | import com.google.api.client.http.javanet.NetHttpTransport; |
12 | import com.google.api.client.http.HttpTransport; |
13 | import com.google.api.client.json.JsonFactory; |
14 | import com.google.api.client.json.jackson2.JacksonFactory; |
15 | import com.google.api.client.util.DateTime; |
16 | import com.google.api.client.util.store.FileDataStoreFactory; |
17 | import com.google.api.services.calendar.Calendar; |
18 | import com.google.api.services.calendar.CalendarScopes; |
19 | import com.google.api.services.calendar.model.Event; |
20 | import com.google.api.services.calendar.model.Events; |
21 | |
22 | private static final String APPLICATION_NAME = "BotCompany Calendar API Demo"; |
23 | private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); |
24 | |
25 | static LS SCOPES = Collections.singletonList(CalendarScopes.CALENDAR_READONLY); |
26 | |
27 | static GoogleClientSecrets getClientSecrets() ctex { |
28 | temp InputStream in = newFileInputStream(javaxSecretDir("google-botcompany-credentials.json")); |
29 | ret GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); |
30 | } |
31 | |
32 | /** |
33 | * Creates an authorized Credential object. |
34 | * @param HTTP_TRANSPORT The network HTTP Transport. |
35 | * @return An authorized Credential object. |
36 | * @throws IOException If the credentials.json file cannot be found. |
37 | */ |
38 | // UNUSED |
39 | static Credential getCredentials(NetHttpTransport HTTP_TRANSPORT) throws IOException { |
40 | GoogleClientSecrets clientSecrets = getClientSecrets(); |
41 | |
42 | // Build flow and trigger user authorization request. |
43 | GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) |
44 | .setDataStoreFactory(new FileDataStoreFactory(mkdirs(programFile("tokens")))) |
45 | .setAccessType("offline") |
46 | .build(); |
47 | LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); |
48 | ret new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); |
49 | } |
50 | |
51 | sS getAccessToken(S authorizationCode) ctex { |
52 | GoogleTokenResponse response = |
53 | new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport, new JacksonFactory, |
54 | googleBotCompanyClientID(), googleBotCompanyClientSecret(), |
55 | authorizationCode, "https://oauth2-login-demo.appspot.com/code") |
56 | .execute(); |
57 | S token = response.getAccessToken(); |
58 | print("Access token: " + token); |
59 | ret token; |
60 | } |
61 | |
62 | static GoogleCredential createCredentialWithRefreshToken(HttpTransport transport, |
63 | JsonFactory jsonFactory, GoogleClientSecrets clientSecrets, S accessToken, S refreshToken) { |
64 | ret new GoogleCredential.Builder().setTransport(transport) |
65 | .setJsonFactory(jsonFactory) |
66 | .setClientSecrets(clientSecrets) |
67 | .build() |
68 | .setAccessToken(accessToken) |
69 | .setRefreshToken(refreshToken); |
70 | } |
71 | |
72 | // pass access token & refresh token as cmd line args! |
73 | p { |
74 | //getAccessToken(args[0]); |
75 | |
76 | GoogleClientSecrets clientSecrets = getClientSecrets(); |
77 | |
78 | NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); |
79 | GoogleCredential credential = createCredentialWithRefreshToken(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, args[0], args[1]); |
80 | |
81 | // Build a new authorized API client service. |
82 | //GoogleCredential credential = new GoogleCredential().setAccessToken(args[0]); |
83 | |
84 | Calendar service = new Calendar.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential) |
85 | .setApplicationName(APPLICATION_NAME) |
86 | .build(); |
87 | |
88 | // List the next 10 events from the primary calendar. |
89 | DateTime now = new DateTime(System.currentTimeMillis()); |
90 | Events events = service.events().list("primary") |
91 | .setMaxResults(10) |
92 | .setTimeMin(now) |
93 | .setOrderBy("startTime") |
94 | .setSingleEvents(true) |
95 | .execute(); |
96 | List<Event> items = events.getItems(); |
97 | if (items.isEmpty()) { |
98 | print("No upcoming events found."); |
99 | } else { |
100 | print("Upcoming events"); |
101 | for (Event event : items) { |
102 | DateTime start = event.getStart().getDateTime(); |
103 | if (start == null) { |
104 | start = event.getStart().getDate(); |
105 | } |
106 | System.out.printf("%s (%s)\n", event.getSummary(), start); |
107 | } |
108 | } |
109 | } |
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: | 371 / 1056 |
Version history: | 14 change(s) |
Referenced in: | [show references] |