Java – Search YouTube using YouTube Data API on Ubuntu No.82

In this blog, how to search YouTube videos using YouTube Data API on Ubuntu is shown. Java application is executed in Visual Studio Code on Ubuntu.

▼1. How to get API Key for YouTube Data API

After creating a project and enabling YouTube Data API v3 API, API key can be generated. please refer to the following documents or another blogs.


▼2. Prerequisites

2-1. Install JDK

Install Java 8 openjdk x64 of zulu

mkdir  -p /usr/lib/jvm/
cd /usr/lib/jvm/
sudo wget https://cdn.azul.com/zulu/bin/zulu8.66.0.15-ca-jdk8.0.352-linux_x64.tar.gz
sudo tar -xzvf zulu8.66.0.15-ca-jdk8.0.352-linux_x64.tar.gz
sudo mv zulu8.66.0.15-ca-jdk8.0.352-linux_x64 java-8-openjdk-linux_x64

Set environment variables

Use vi editor like “vi ~/.bashrc” and add below in ~/.bashrc

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-x64/

Check the value of $JAVA_HOME

echo $JAVA_HOME

2-2. Install Visual Studio Code

https://code.visualstudio.com/docs/setup/linux

sudo snap install --classic code

2-3. Install Maven

Apache Kafka Word Count – Java No.44 “2-3. Installing Apache Maven”


▼3. Java code to search videos using YouTube Data API

3-1. Create an Apache Maven project

mvn archetype:generate -DinteractiveMode=false -DgroupId=org.example.youtubetest -DartifactId=youtubesearch -DarchetypeArtiFactId=maven-archetype-quickstart

3-2. Remove the existed both App.java and AppTest.java

rm ./youtubesearch/src/main/java/org/example/youtubetest/App.java 
rm ./youtubesearch/src/test/java/org/example/youtubetest/AppTest.java

3-3. Start Visual Studio Code

cd ./youtubeapi/youtubesearch
code .

3-4. Create youtube.properties

Create youtube.properties file and add API KEY that is created before. “YOUR_SIMPLE_API_KEY_HERE” should be updated to your API KEY.

cd ./youtubeapi/youtubesearch
mkdir -p ./src/main/resources/
cd ./src/main/resources/
code youtube.properties
# youtube.properties の中身
youtube.apikey=YOUR_SIMPLE_API_KEY_HERE
youtube.properties

3-5. Update pom.xml as below and save it (Ctrl+S)

Java コード サンプル  |  YouTube Data API  |  Google Developers の pom.xml を参照

xxx
  <properties>
    <project.youtube.version>v3-rev8-1.12.0-beta</project.youtube.version>
    <project.http.version>1.12.0-beta</project.http.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <repositories>
    <repository>
      <id>google-api-services</id>
      <url>http://google-api-client-libraries.appspot.com/mavenrepo</url>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- YouTube Data V3 support -->
    <dependency>
      <groupId>com.google.apis</groupId>
      <artifactId>google-api-services-youtube</artifactId>
      <version>${project.youtube.version}</version>
    </dependency>
    <dependency>
      <groupId>com.google.http-client</groupId>
      <artifactId>google-http-client-jackson2</artifactId>
      <version>${project.http.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <configuration>
          <mainClass>com.google.api.services.samples.youtube.cmdline.youtube_cmdline_search_sample.Search</mainClass>
        </configuration>
      </plugin>

      <!-- Forces Maven to use Java 1.6 -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <compilerArgument></compilerArgument>
        </configuration>
      </plugin>
    </plugins>
  </build>

xxx

3-6. Develop youtubesearch.java

Copy this java code from Search.java in the following site. in my case, since the class name was youtubesearch, I updated the Java code from Search to youtubesearch.

Java コード サンプル  |  YouTube Data API  |  Google Developers

3-7. Run youtubesearch.java

Execute youtubesearch.java in the terminal and then the search term is required.

In this case, I will search my posted YouTube videos regarding Robo Advisor WealthNavi. I put the key word “Investment Watcher WealthNavi” as below.

Please enter a search term: Investment Watcher WealthNavi

=============================================================
   First 25 videos for search on "Investment Watcher WealthNavi".
=============================================================

 Video Idyb7MAadW-EE
 Title: 2020/03【答え合わせ】ロボアドバイザーWealthNavi の運用実績 No3 Let’s show the results of WealthNavi
 Thumbnail: https://i.ytimg.com/vi/yb7MAadW-EE/default.jpg

-------------------------------------------------------------

 Video Id1BNgED4goGY
 Title: 2020/02【答え合わせ】ロボアドバイザーWealthNavi の運用実績 No2 - Let’s show the results of  WealthNavi
 Thumbnail: https://i.ytimg.com/vi/1BNgED4goGY/default.jpg

-------------------------------------------------------------

 Video IdFO7senEUYGs
 Title: 2020/04【答え合わせ】ロボアドバイザーWealthNavi の運用実績 No4- The results of playing the stock market by WealthNavi
 Thumbnail: https://i.ytimg.com/vi/FO7senEUYGs/default.jpg

-------------------------------------------------------------

 Video IdBr6qlyb7z3E
 Title: 2020/05【答え合わせ】ロボアドバイザーWealthNavi の運用実績 No5 - The results of playing the stock market by WealthNavi
 Thumbnail: https://i.ytimg.com/vi/Br6qlyb7z3E/default.jpg

-------------------------------------------------------------

 Video Id-5-eGf7mOPo
 Title: 2020/01 - WealthNavi の運用実績を紹介! No1 - Let’s introduce the result of
 Thumbnail: https://i.ytimg.com/vi/-5-eGf7mOPo/default.jpg

-------------------------------------------------------------

 Video Id4cd_AbDWAOk
 Title: 【答え合わせ】 ロボアドバイザー WealthNavi の運用実績!No12 -2020/12 上がりすぎ !? Much higher than a public value
 Thumbnail: https://i.ytimg.com/vi/4cd_AbDWAOk/default.jpg

-------------------------------------------------------------

xxx

3-8. Check the link of YouTube using Video id

For the following example, Video Id is 4cd_AbDWAOk and copy 4cd_AbDWAOk and add it into the suffix of https://www.youtube.com/watch?v=.

(A part of output)

Video Id4cd_AbDWAOk
 Title: 【答え合わせ】 ロボアドバイザー WealthNavi の運用実績!No12 -2020/12 上がりすぎ !? Much higher than a public value
 Thumbnail: xxxx

The following URL is created and check it https://www.youtube.com/watch?v=4cd_AbDWAOk



▼4. Reference

  1. Install VS Code https://code.visualstudio.com/docs/setup/linux
  2. Apache Kafka Word Count – Java No.44 “2-3. Installing Apache Maven”
  3. Java code sample  |  YouTube Data API  |  Google Developers
  4. YouTube Data API  |  Google Developers
  5. Obtaining authorization credentials

That’s all. Have a nice day ahead !!!


Leave a Reply

Your email address will not be published. Required fields are marked *