Get holidays with the Google Calendar API

Article summary

--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

background

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.

Purpose

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.

procedure

-Official sample Reference ――The repository I actually created is this

1. Create a Maven project (Refer to 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}

2. Download the Credential file

-On the Official Sample page

  1. Click "ENABLE GOOGLE CALENDAR API"
スクリーンショット_2018-11-04_14_09_17.png 2. Click "Select or create project" スクリーンショット_2018-11-04_14_09_54.png 3. Select an existing project or select "Create a new project" and enter the project name 4. Click "DOWNLOAD CLIENT CONFIGURATION" スクリーンショット_2018-11-04_14_09_39.png 5. Place the downloaded `json` file in the` src / main / resources` folder in the project (probably the `resources` folder does not exist, so create a new one) 6. Rename the placed `json` file to` credentials.json`

3. Copy the source

  1. Rename the ʻApp.java file in src / maintoCalendarQuickstart.java`
  2. Go to CalendarQuickstart.java Copy the contents of CalendarQuickstart.java in Official Sample.

4. Edit the source file

  1. Add the package name to the first line of 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)
  2. Change the argument passed to 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)
スクリーンショット_2018-11-04_14_30_55.png

5. Edit pom.xml

  1. Add the following dependency
  1. Add ʻexec-maven-plugin 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>

6. Run

  1. Run mvn clean install in the terminal
  2. Execute mvn exec: java
  3. A list of 10 holidays after the execution date is output (read the source and official document for details)
Screenshot 2018-11-04 14.53.17.png 4. After that, if you want to reflect the source change, execute `mvn compile` and then` mvn exec: java`.

I was addicted to

Write the points you are addicted to below

I get a 401 Unauthorized Error

com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized

Reason

Because the token has expired

Countermeasures

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)

Events not found with No upcoming events found.

Reason

The argument passed to service.events (). List () in the main method of src / main / CalendarQuickstart.java is primary.

Countermeasures

Change primary to [email protected]

Recommended Posts

Get holidays with the Google Calendar API
Get information with zabbix api
Create a tweet heatmap with the Google Maps API
Get comments and subscribers with the YouTube Data API
Call the API with python3.
Get ranking with Rakuten API
Throw appointments to others with the LINE WORKS calendar API
Get data from analytics API with Google API Client for python
Get reviews with python googlemap api
Specifying the date with the Twitter API
[Python] Hit the Google Translation API
Get the weather with Python requests
Get the weather with Python requests 2
Hit the Etherpad-lite API with Python
Introducing Google Map API with rails
Get the number of PVs of Qiita articles you posted with API
Get conversions and revenue with Google Analytics API and report to Slack
Get Keyword Planner Search Volume / CPC / Competitiveness with Google Ads API
Access the Docker Remote API with Requests
Streaming speech recognition with Google Cloud Speech API
Get the minutes of the Diet via API
Get Google Fit API data in Python
Get started with the documentation tool Sphinx
Access Google Calendar from the iOS app
[Python] Get Python package information with PyPI API
Play with puns using the COTOHA API
Get coincheck virtual currency information with API ♪
I tried to get the authentication code of Qiita API with Python.
Get the number of articles accessed and likes with Qiita API + Python
I tried to get the movie information of TMDb API with Python
Tweet regularly with the Go language Twitter API
[Python] Get the files in a folder with Python
Get the latest AMI information with the AWS CLI
Golang api get
I tried "License OCR" with Google Vision API
Use the MediaWiki API to get Wiki information
Until you use the Kaggle API with Colab
Get stock price data with Quandl API [Python]
I tried using the Google Cloud Vision API
How to use the Google Cloud Translation API
Get an access token for the Pocket API
I tried "Receipt OCR" with Google Vision API
Get Gmail subject and body with Python and Gmail API
Get data labels by linking with Google Cloud Vision API when previewing images with Rails
A story about a Python beginner trying to get Google search results using the API
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
[First API] Try to get Qiita articles with Python
Import the schedule obtained from "Schedule-kun" into Google Calendar
Play with the power usage API provided by Yahoo
[Python] Get the text of the law from the e-GOV Law API
Get the sum of each of multiple columns with awk
Hit the Twitter API after Oauth authentication with Django
[Python] Get the numbers in the graph image with OCR
Get the image of "Suzu Hirose" by Google image search.
Implement Sign In With Google on the backend side
Get schedule from Garoon SOAP API with Python + Zeep
[Boto3] Search for Cognito users with the List Users API
Get the result in dict format with Python psycopg2
[Python] Get insight data using Google My Business API
Get swagger.json with Flask-RESTX (Flask-RESTPlus) without starting the server
Try to get the contents of Word with Golang