java - Youtube data api v3 搜索特定 channel 所有上传和发布的视频

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

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

在 youtube.com 中,我可以在 channel 的“视频”选项卡中看到两种视频

  • 发布的视频(由其他 channel 上传)

  • 上传的视频(由该 channel 上传)

通过api搜索时,通过设置特定的channelId,api只返回该 channel 上传的视频。 有什么办法也可以获取发布的视频吗?

    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();
        }
    }

最佳答案

通过使用Search: list指定channelId,您将得到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"
   }
  },

你会注意到第一个结果的标题是“Kobe's Farewell Tour”,通过获取这个视频,我发现这是一个播放列表,如果你检查它的内容,它由不同的内容组成不同用户上传的视频。

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

enter image description here enter image description here

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

希望对您有帮助。

关于java - Youtube data api v3 搜索特定 channel 所有上传和发布的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42312939/

相关文章:

java - 按下某个键时重新绘制 JPanel。 ( java )

html - 如何获取自动播放YouTube视频的嵌入式代码

Android - 在 SurfaceTexture 上绘制 YouTube 视频

html - CSS 无法更改 div 位置,这会导致裁剪

youtube - YouTube stopVideo()是暂停而不是停止吗?

java - 将 SoapUI 属性值设置为今天 + 1 年

java - 清空 Nutch 抓取列表

ajax - Ajax YouTube上传

api - YouTube API V3,youtube.playlistItems.list,参数ID与播放列表ID

java - 使用 Xposed Framework Hook BroadcastReceiver