如何使用搜索 youtube videoId api 调用来获取 youtube 标签?

问题描述 投票:0回答:1

我需要使用 youtube api 视频 id api 从片段中获取标签。 请参阅以下响应和日志。我在回复中没有找到标签。因此它将标签打印为空。如何标记片段中的信息?

示例代码:

public static void printVideo(int index, @NonNull Video video) {
        VideoSnippet snippet = video.getSnippet();
       log.trace("Video[" + index + "] Tags = {}", snippet.getTags());
}

测试日志:

    youtube.log:[Test worker] TRACE com.ct.youtube.util.YouTubeUtil - Video[0] Tags = null

示例响应:

{
  "contentDetails": {
    "duration": "PT5M28S"
  },
  "snippet": {
    "channelId": "UCVfwlh9XpX2Y_tQfjeln9QA",
    "channelTitle": "BibleProject",
    "description": "The wisest king of Israel, King Solomon, is associated with three books of the Bible: Proverbs, Ecclesiastes, and the Song of Songs. Each book offers a unique perspective on how humans can rule with wisdom and the fear of the Lord. In this video, we briefly explore how the message of each book fits into the overall story of the Bible.\n\n#Solomon #Proverbs #Wisdom",
    "publishedAt": "2019-06-27T22:09:15.000Z",
    "thumbnails": {
      "default": {
        "url": "https://i.ytimg.com/vi/WJgt1vRkPbI/default.jpg"
      }
    },
    "title": "The Books of Solomon"
  },
  "statistics": {
    "commentCount": "1839",
    "favoriteCount": "0",
    "likeCount": "37779",
    "viewCount": "1213094"
  }
}
java youtube-api
1个回答
0
投票

确保您在 Google Cloud Console 中的项目具有正确的权限,并且 YouTube Data API v3 已启用。

import com.google.api.services.youtube.*;

import java.io.IOException;
import java.util.List;

public class YouTubeVideoFetcher {
    private static final String API_KEY = "YOUR_API_KEY";

    public static void main(String[] args) throws IOException {
        YouTube youtubeService = new YouTube.Builder(
            new com.google.api.client.http.javanet.NetHttpTransport(),
            new com.google.api.client.json.jackson2.JacksonFactory(),
            request -> {}
        ).setApplicationName("youtube-tags-fetcher").build();

        YouTube.Videos.List request = youtubeService.videos()
            .list("snippet")
            .setId("YOUR_VIDEO_ID") // Replace with the actual video ID
            .setKey(API_KEY);
        VideoListResponse response = request.execute();

        List<Video> videos = response.getItems();
        if (!videos.isEmpty()) {
            for (int i = 0; i < videos.size(); i++) {
                VideoSnippet snippet = videos.get(i).getSnippet();
                List<String> tags = snippet.getTags();
                System.out.println("Video[" + i + "] Tags = " + (tags != null ? tags : "No tags found"));
            }
        } else {
            System.out.println("No videos found for the given ID.");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.