Java Lib with useful services for Google Calendar API.
- Maven
- Java 1.8
- Lombok
- Google Calendar API
- Spring Contex
- Static Code Analysis: SonarCloud
- Analyze SonarCloud
- Build with Maven (branch master)
- Publish on GitHub Packages (tag/release)
Read and create events on Google Calendar.
To check PMD rules in your machine, run follow command in app
dir:
mvn pmd:check
To include this dependency in you project, you need:
- Add dependency in your
pom.xml
:
<dependency>
<groupId>br.com.bvilela.lib</groupId>
<artifactId>google-calendar-util-lib</artifactId>
<version>1.0.0</version>
</dependency>
- Add the GitHub repository in your
pom.xml
:
<repositories>
<repository>
<id>github</id>
<name>GitHub</name>
<url>https://maven.pkg.github.com/bvilela/google-calendar-util-lib</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
- Add the authentication to the Package Registry in your global
settings.xml
:USER_HOME\.m2\settings.xml
<servers>
<server>
<id>github</id>
<username>YOUR_USERNAME</username>
<password>YOUR_AUTH_TOKEN</password>
</server>
</servers>
Replace the YOUR_USERNAME
with your GitHub login name.
Replace the YOUR_AUTH_TOKEN
with a generated GitHub Personal Access Token (PAT):
GitHub > Settings > Developer Settings > Personal access tokens > Generate new token.
The token needs at least the
read:packages
scope.❗ Otherwise you will get a Not authorized exception.
For generate ClientId and ClientSecret, see these documentations:
- Java Quickstart
- Create a Google Cloud project
- Create access credentials
- Create an OAuth client ID: App Type Desktop;
- Download the json file;
- Rename the file to
google-credentials.json
; - Copy the file to
resources
oranother directory
(see this section);
- Remember to add your gmail in
Test Users
in your Project in Google Cloud Console- Add following scopes in your app:
https://www.googleapis.com/auth/calendar
https://www.googleapis.com/auth/calendar.events
In your Application Class add:
@SpringBootApplication
@ComponentScan({"<your-path>", "br.com.bvilela.lib"})
public class Application {
}
This make your Spring Application recognise the Spring Services of lib.
To create a event use:
import br.com.bvilela.lib.enuns.ColorEnum;
import br.com.bvilela.lib.model.CalendarEvent;
import br.com.bvilela.lib.service.GoogleCalendarCreateService;
@Autowired
private GoogleCalendarCreateService calendarService;
CalendarEvent dto = CalendarEvent.builder()
.setSummary("My Event Title")
.setLocation("My Event Location")
.setDescription("My Event Description")
.setDateTimeStart(LocalDateTime.of(2022, 6, 20, 20, 0))
.setDateTimeEnd(LocalDateTime.of(2022, 6, 20, 21, 0))
.setColor(ColorEnum.SALVIA) // Default: ColorEnum.PADRAO
.setTimeZone("America/Sao_Paulo") // Default: "America/Sao_Paulo"
.build();
calendarService.createEvent(dto);
// or
calendarService.createEvents(List.of(dto1, dto2...));
To enable default logging of events sent to Google Calendar, follow these steps:
- In your
application.properties
add:
bvilela.lib.google.calendar.log.enabled=true
Example Default Log Output
Sending Event to Google Calendar...
CalendarEvent[Summary=myEventTitle, Dates=(2022-07-19T21:30 - 2022-07-19T22:00), Color=SALVIA]
If you don't want or can't add the google-credentials.json
file in the src
directory, add this property
in your application.properties
and set the directory
to save the file.
bvilela.lib.google.calendar.credentials.path=D:\\my-directory