Youtube数据api v3按特定频道搜索所有上传和发布的视频

时间:2017-02-18 08:32:08

标签: java youtube youtube-api youtube-data-api

我正在使用Youtube数据API版本3.使用提供的java代码搜索特定频道内的所有视频。

在youtube.com中,我可以在频道的“视频”标签中看到两种视频

  • 发布视频(由其他渠道上传)

  • 上传的视频(通过此频道上传)

在搜索api时,通过设置特定的channelId,api仅返回该频道上传的视频。 有什么方法可以发布视频吗?

    public static void main(String[] args) {
        // Read the developer key from the properties file.
        Properties properties = new Properties();
        try {
            InputStream in = Search.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
            properties.load(in);

        } catch (IOException e) {
            System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause()
                    + " : " + e.getMessage());
            System.exit(1);
        }

        try {
            // This object is used to make YouTube Data API requests. The last
            // argument is required, but since we don't need anything
            // initialized when the HttpRequest is initialized, we override
            // the interface and provide a no-op function.
            youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
                public void initialize(HttpRequest request) throws IOException {
                }
            }).setApplicationName("youtube-cmdline-search-sample").build();

            // Prompt the user to enter a query term.
            String queryTerm = getInputQuery();

            // Define the API request for retrieving search results.
            YouTube.Search.List search = youtube.search().list("id,snippet");

            // Set your developer key from the {{ Google Cloud Console }} for
            // non-authenticated requests. See:
            // {{ https://cloud.google.com/console }}
            String apiKey = properties.getProperty("youtube.apikey");
            search.setKey(apiKey);
            search.setQ(queryTerm);
            search.setChannelId("UCEgdi0XIXXZ-qJOFPf4JSKw");

            // Restrict the search results to only include videos. See:
            // https://developers.google.com/youtube/v3/docs/search/list#type
            search.setType("video");

            // To increase efficiency, only retrieve the fields that the
            // application uses.
            search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
            search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);

            // Call the API and print results.
            SearchListResponse searchResponse = search.execute();
            List<SearchResult> searchResultList = searchResponse.getItems();
            if (searchResultList != null) {
                prettyPrint(searchResultList.iterator(), queryTerm);
            }
        } catch (GoogleJsonResponseException e) {
            System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
                    + e.getDetails().getMessage());
        } catch (IOException e) {
            System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:2)

通过使用指定channelId的Search: list,您将得到248个结果。这意味着这些结果是用户上传的视频。但是,这并不意味着他拥有它。

为了更好地解释,我使用此参数。

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=snippet&channelId=UCEgdi0XIXXZ-qJOFPf4JSKw&_h=1&

我使用您在问题中指定的channelId。我们将得到第一个这样的结果。

"snippet": {
    "publishedAt": "2015-12-03T17:14:46.000Z",
    "channelId": "UCEgdi0XIXXZ-qJOFPf4JSKw",
    "title": "Kobe's Farewell Tour",
    "description": "Kobe Bryant announced that this season, his 20th, will be his last, and is saying goodbye to fans around the league.",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/FR0AqkteAYw/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/FR0AqkteAYw/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/FR0AqkteAYw/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Sports",
    "liveBroadcastContent": "none"
   }
  },

你会注意到第一个结果的标题是“科比的告别之旅”,通过观看这个视频,我发现这是一个播放列表,如果你查看它的内容,它会组成不同的由不同用户上传的视频。

https://www.youtube.com/watch?v=FR0AqkteAYw&list=PL8fVUTBmJhHLB3FW_53W1P0mtmwRTCEK_

enter image description here enter image description here

所以这些是你在channelId = UCEgdi0XIXXZ-qJOFPf4JSKw中看到的视频。因此,要获取所有视频,请使用您在search.list中获得的所有播放列表中的PlaylistItems: list

希望它对你有所帮助。