YouTube数据API按ID列出视频

时间:2016-06-17 19:12:39

标签: youtube-api youtube-data-api

我使用youtube api-samples java代码按ID列出视频。我使用了Topics.java文件,但它提供了多个结果,包括正确的结果。此外,它不应用视频ID(当我打印它时视频ID为0)所以我也给了搜索词的视频ID。下面是输入和输出的屏幕截图。

enter image description here

我想要实现的是获取给定视频ID的确切结果,例如它如何在YouTube Data API code snippet上运行。

我运行错误的代码还是输错了?请指教。

更新:文件VideoLocalizations.java为“语言”抛出空指针异常 enter image description here

1 个答案:

答案 0 :(得分:2)

Topics.java文件正在演示Topics API,我认为这比您想做的更多。

要简单了解视频ID的详细信息,请查看Videos.list。您可以使用页面底部的API Explorer来创建基本请求,例如

GET https://www.googleapis.com/youtube/v3/videos?
    part=id%2Csnippet&id=5vY8EWokf40&key={YOUR_API_KEY}

...返回:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/v-nx5E3jmblZ7cA3yNCjAAKmywQ\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {

   "kind": "youtube#video",
   "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/rsLwePwmFcmdkMVDPrQB20sLp1Q\"",
   "id": "5vY8EWokf40",
   "snippet": {
    "publishedAt": "2015-06-27T03:02:46.000Z",
    "channelId": "UCz0Am9KlCDydaIifIL16hfw",
    "title": "Hajar Film - Sirin Hamsho | فيلم هاجر - سيرين حمشو",
...

要在Java中执行此操作,请查看同一页面上的“示例”部分。从本质上讲,您需要创建一个com.google.api.services.youtube.YouTube对象,调用videos().list(),然后处理VideoListResponse

查看JAVA#2示例,您可以看到以下内容:

// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
    .setApplicationName("youtube-cmdline-localizations-sample").build();

...

// Call the YouTube Data API's videos.list method to retrieve videos.
VideoListResponse videoListResponse = youtube.videos().
    list("snippet,localizations").setId(videoId).execute();

// Since the API request specified a unique video ID, the API
// response should return exactly one video. If the response does
// not contain a video, then the specified video ID was not found.
List<Video> videoList = videoListResponse.getItems();
if (videoList.isEmpty()) {
    System.out.println("Can't find a video with ID: " + videoId);
    return;
}
Video video = videoList.get(0);

有关使用Java库的更多详细信息,请查看JavaDoc reference for the YouTube Data API