Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active May 10, 2022 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/b0078a63655cbe08b7295b671a06d014 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/b0078a63655cbe08b7295b671a06d014 to your computer and use it in GitHub Desktop.
Create, Update or Delete Google Calendar in Java
OAuthUser user = new OAuthUser();
// Set clientId, clientSecret and email
user.clientId = "<<clientID>>";
user.clientSecret = "<<clientSecret>>";
user.email = "<<email>>";
// You have to retrieve AuthorizationCode manually with generated AuthorizationCodeUrl
// Set authorizationCode
String authorizationCode = "<<authCode>>";
// Copy Code Verifier from the previous step output
// Set codeVerifier
String codeVerifier = "<<codeVerifier>>";
// Get Refresh Token
String refreshToken = GoogleOAuthHelper.getAccessTokenByAuthCode(authorizationCode, codeVerifier, user);
user.refreshToken = refreshToken;
// Get Access Token
String accessToken = GoogleOAuthHelper.getAccessTokenByRefreshToken(user);
// Create Gmail client
try (IGmailClient client = GmailClient.getInstance(accessToken, user.email)) {
// Insert, get and update calendar
Calendar calendar = new Calendar("Summary", "Description", "Location", "America/Los_Angeles");
// Insert calendar and Retrieve same calendar using id
String id = client.createCalendar(calendar);
}
// Create Gmail client
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Access and delete calendar with summary starting from "Calendar summary"
String summary = "Calendar summary";
// Get calendars list
ExtendedCalendar[] lst = client.listCalendars();
for (ExtendedCalendar extCal : lst) {
// Delete selected calendars
if (extCal.getSummary().startsWith(summary))
client.deleteCalendar(extCal.getId());
}
}
// Create Gmail client
try (IGmailClient client = GmailClient.getInstance(accessToken, email)) {
// Specify calendar ID
String id ="<<calendar ID>>"
// Fetch calendar
Calendar cal = client.fetchCalendar(id);
// Change information in the fetched calendar and update calendar
cal.setDescription("New Description");
cal.setLocation("New Location");
// Update calendar
client.updateCalendar(cal);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment