--How to get holidays using Google Calendar API in Maven --You can use it by playing with the official sample code --Tokens are reset regularly
Since the Google Calendar API is famous, it's easy to find a way to use it, but I've had a little trouble with PHP and Python, so I'll write it for someone similar to me.
Rewrite the official sample code for Maven to get holidays. A weak engineer who is a new graduate for half a year writes the contents in an easy-to-understand manner.
-Official sample Reference ――The repository I actually created is this
--You should have a src
folder and pom.xml
in your project
$ mvn archetype:generate \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false \
-DgroupId={com.ikeponias} \
-DartifactId={google-calendar-sample}
-On the Official Sample page
file in
src / mainto
CalendarQuickstart.java`CalendarQuickstart.java
Copy the contents of CalendarQuickstart.java
in Official Sample.src / main / CalendarQuickstart.java
(It depends on whether the folder hierarchy has been changed, but basically the groupId when it was first generated. I ampackage com.ikeponias;
And add)service.events (). List ()
in the main
method of src / main / CalendarQuickstart.java
from primary
to [email protected]
. (Because it is an API to get calendar events, your own event will appear if you do not do this, Reference)pom.xml
dependency
to
plugin`--To call the main function from the command line
--Specify java
for goal
--Specify the location of CalendarQuickstart.java
for mainClass
(com.ikeponias.CalendarQuickstart
in my case)
Below is my example (junit is written when the project is created)
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client -->
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.26.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-jetty -->
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.26.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.apis/google-api-services-calendar -->
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-calendar</artifactId>
<version>v3-rev355-1.25.0</version>
</dependency>
<!--Should already exist when the project is created-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- https://mvnrepository.com/artifact/org.codehaus.mojo/exec-maven-plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.ikeponias.CalendarQuickstart</mainClass>
</configuration>
</plugin>
</plugins>
</build>
mvn clean install
in the terminalmvn exec: java
Write the points you are addicted to below
401 Unauthorized Error
com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
Because the token has expired
Delete the tokens
folder and re-execute (it may be good to include a process to delete the tokens
folder at the time of execution)
No upcoming events found.
The argument passed to service.events (). List ()
in the main
method of src / main / CalendarQuickstart.java
is primary
.
Change primary
to [email protected]
Recommended Posts