LoginSignup
36
21

More than 3 years have passed since last update.

YouTube Data API v3 を使って YouTube 動画を検索する

Posted at

前提

GCP にアカウント登録済であることを前提とします。

準備

GCP にアクセスします。
ログインしていない場合はしてください。

プロジェクトを作成

まずは今回使用するプロジェクトを作成します。
プロジェクトの選択 をクリックします。
select-project-1.png


新しいプロジェクト をクリックします。
create-project-2.png


プロジェクト名 に好きな名前を入力してください。
今回は youtube-data-api-v3-example とします。
入力したら 作成 をクリックします。
create-project-3.png

これでプロジェクトが作成されます。

API を有効化

次に、プロジェクト内で YouTube Data API v3 を有効化します。
メニューから API とサービス -> ダッシュボード をクリックします。
enable-api-1.png


API とサービスの有効化 をクリックします。
enable-api-2.png


API の検索画面になります。
YouTube Data API v3 を検索してクリックします。
API-ライブラリ-youtube-data-api-v3-example-Google-Cloud-Platform.png
enable-api-3.png


有効化する をクリックします。
enable-api-4.png

これでプロジェクト内で YouTube Data API v3 が有効になりました。

API キーを発行する

最後に、認証に使用する API キーを発行します。
左のメニューから 認証情報 を選択し、 認証情報を作成 -> API キー の順にクリックします。
create-api-key-1.png


API キーが発行されるので、控えておいてください。
今回は AIzaSyBNbBNJjMafqIP651cERFKkw_3fBC7bG_U ですね。
引き続き キーを制限 をクリックします。
create-api-key-2.png


名前 には API キーの用途がわかりやすい名前を入力してください。
今回は for youtube data api v3 とします。
キーの制限 をチェックして YouTube Data API v3 を選択します。
( ※ キーを制限しない のままでも使用できますが、基本的に API キーの用途はなるべく制限しておいた方が無難です。 )
create-api-key-4.png
create-api-key-5.png


最後に 保存 をクリックします。
create-api-key-6.png

これで YouTube Data API v3 を使用する準備ができました。

YouTube 動画を検索する

検索には Search.list メソッド を使用します。
エンドポイントは https://www.googleapis.com/youtube/v3/search です。

パラメータ名 説明
key 先程発行した API キー。
type 検索するリソースのタイプ。 channel, playlist, video を指定できます。
今回は動画を検索するので video を指定します。
part レスポンスに含めるリソースのプロパティを指定します。 id, snippet を指定できます。例えば snippet を指定するとレスポンスに動画の ID だけじゃなく、タイトルや説明が含まれるようになります。
今回は snippet を指定します。
q 検索クエリ。
今回は適当に dog にしておきます。

各パラメータの詳細やその他指定できるパラメータについては リファレンス を参照してください。

curlを使用した例
$ curl 'https://www.googleapis.com/youtube/v3/search?key=AIzaSyBNbBNJjMafqIP651cERFKkw_3fBC7bG_U&type=video&part=snippet&q=dog'
レスポンス例
{
  "kind": "youtube#searchListResponse",
  "etag": "PxTg2f__5ts4eJ6tAwUYSefueJ8",
  "nextPageToken": "CAUQAA",
  "regionCode": "JP",
  "pageInfo": {
    "totalResults": 1000000,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "rMGmCfFQdus1PC5C8u59njhfN60",
      "id": {
        "kind": "youtube#video",
        "videoId": "wtH-hdOF1uA"
      },
      "snippet": {
        "publishedAt": "2020-02-08T15:32:50Z",
        "channelId": "UC8hC-augAnujJeprhjI0YkA",
        "title": "Baby Dogs - Cute and Funny Dog Videos Compilation #20 | Aww Animals",
        "description": "Watching funny baby dogs is the hardest try not to laugh challenge. Baby dogs are amazing creature because they are the cutest and most funny. This is the ...",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/wtH-hdOF1uA/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/wtH-hdOF1uA/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/wtH-hdOF1uA/hqdefault.jpg",
            "width": 480,
            "height": 360
          }
        },
        "channelTitle": "Aww Animals",
        "liveBroadcastContent": "none",
        "publishTime": "2020-02-08T15:32:50Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "-zC8lkAkOs8V_ZsAM23wxd09MB4",
      "id": {
        "kind": "youtube#video",
        "videoId": "1HygThMLzGs"
      },
      "snippet": {
        "publishedAt": "2020-05-05T09:34:51Z",
        "channelId": "UCIqWFhsJm2VU1ZuGrZnx3Pw",
        "title": "🤣Funny Dog Videos 2020🤣 🐶 It's time to LAUGH with Dog's life",
        "description": "Funny Dog Videos 2020🤣 It's time to LAUGH with Dog's life Thanks For Watching ! Please Like Share & Comment If You Like This Video ! Subscribe for new ...",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/1HygThMLzGs/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/1HygThMLzGs/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/1HygThMLzGs/hqdefault.jpg",
            "width": 480,
            "height": 360
          }
        },
        "channelTitle": "MAI PM",
        "liveBroadcastContent": "none",
        "publishTime": "2020-05-05T09:34:51Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "eQ0T7tVTBZAl9L9p-09yRexq-50",
      "id": {
        "kind": "youtube#video",
        "videoId": "lyelxGIydeQ"
      },
      "snippet": {
        "publishedAt": "2020-01-13T14:07:51Z",
        "channelId": "UCvznoyf6e8T1NRHLkfxwG9Q",
        "title": "Cute Dogs and Babies are Best Friends - Dogs Babysitting Babies Video",
        "description": "Video: Cute Dogs and Babies are Best Friends - Dogs Babysitting Babies Video ☞ Link video: https://youtu.be/lyelxGIydeQ Thanks for watching! Wait to see ...",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/lyelxGIydeQ/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/lyelxGIydeQ/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/lyelxGIydeQ/hqdefault.jpg",
            "width": 480,
            "height": 360
          }
        },
        "channelTitle": "Baby Awesome",
        "liveBroadcastContent": "none",
        "publishTime": "2020-01-13T14:07:51Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "kg5QEV7h_g8HzqoGw59bB7eJSUI",
      "id": {
        "kind": "youtube#video",
        "videoId": "oRPMgn6mIKs"
      },
      "snippet": {
        "publishedAt": "2021-01-11T02:01:09Z",
        "channelId": "UCVo6gkfRE7y1sigGJ82bTCA",
        "title": "To The Hell - Short Pencilmated Animation Cartoons",
        "description": "To The Hell - Short Pencilmated Animation Cartoons Max's Puppy Dog is traditional frame-by-frame 2D animation channel with many videos telling stories ...",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/oRPMgn6mIKs/default_live.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/oRPMgn6mIKs/mqdefault_live.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/oRPMgn6mIKs/hqdefault_live.jpg",
            "width": 480,
            "height": 360
          }
        },
        "channelTitle": "Max's Puppy Dog",
        "liveBroadcastContent": "live",
        "publishTime": "2021-01-11T02:01:09Z"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "XI59xy0dadeP9nii-Gs85-32aEA",
      "id": {
        "kind": "youtube#video",
        "videoId": "kdJvv3RipNY"
      },
      "snippet": {
        "publishedAt": "2020-09-29T15:15:02Z",
        "channelId": "UC5-Vfq-dFfj6R77-OFMEn7w",
        "title": "Funny Videos Animals - 🐶 It's time to LAUGH with Dog's life - Dog 2020",
        "description": "Link video: https://youtu.be/kdJvv3RipNY Subscribe: https://bit.ly/2Tft6xq LIFE FUNNY PETS brings you funny cat and dog videos and also cute animals in the ...",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/kdJvv3RipNY/default.jpg",
            "width": 120,
            "height": 90
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/kdJvv3RipNY/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/kdJvv3RipNY/hqdefault.jpg",
            "width": 480,
            "height": 360
          }
        },
        "channelTitle": "Life Funny Pets",
        "liveBroadcastContent": "none",
        "publishTime": "2020-09-29T15:15:02Z"
      }
    }
  ]
}

まとめ

簡単!便利!

参考

36
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
21