【问题标题】:Getting All Uploaded Videos获取所有上传的视频
【发布时间】:2015-08-22 03:47:07
【问题描述】:

我需要一个 Java 方法,它为我提供了一个列表,其中包含上传到我自己的 YouTube 频道上的所有 YouTube 观看网址(或视频 ID)。由于应该包含私人和未列出的视频,因此必须通过用户名和密码或OAuth 2.0 进行身份验证。

另外,我知道YouTube API,但我仍然不知道如何做到这一点。任何工作代码 sn-ps 例如没有像here 这样的模糊描述,因为当一切都必须自动化时,它就没有用了。

我基本上需要这样的方法:

public static List<String> getAllUploadedVideos(String channelUrl)
{
    // use OAuth 2.0 to gain viewing permissions for the YouTube account
    // get all uploaded videos
}

Here 可以进行搜索查询。

【问题讨论】:

  • 抱歉,StackOverflow 不是代码编写服务。这是一个询问人们在编写自己的代码时遇到的问题的地方。

标签: java video youtube


【解决方案1】:
public static List<String> getUploadedVideoIds() throws IOException
{
    YouTube youtube;
    List<String> scopes = Lists
            .newArrayList("https://www.googleapis.com/auth/youtube.readonly");
    Credential credential = Auth.authorize(scopes, "myuploads");

    youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY,
            credential).setApplicationName(
            "youtube-cmdline-myuploads-sample").build();

    YouTube.Channels.List channelRequest = youtube.channels().list(
            "contentDetails");
    channelRequest.setMine(true);
    channelRequest.setFields("items/contentDetails,nextPageToken,pageInfo");
    ChannelListResponse channelResult = channelRequest.execute();

    List<Channel> channelsList = channelResult.getItems();

    String uploadPlaylistId = channelsList.get(0).getContentDetails()
            .getRelatedPlaylists().getUploads();

    List<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();

    YouTube.PlaylistItems.List playlistItemRequest = youtube
            .playlistItems().list("id,contentDetails,snippet");
    playlistItemRequest.setPlaylistId(uploadPlaylistId);

    playlistItemRequest
            .setFields("items(contentDetails/videoId),nextPageToken,pageInfo");

    String nextToken = "";

    do
    {
        playlistItemRequest.setPageToken(nextToken);
        PlaylistItemListResponse playlistItemResult = playlistItemRequest
                .execute();

        playlistItemList.addAll(playlistItemResult.getItems());

        nextToken = playlistItemResult.getNextPageToken();
    } while (nextToken != null);

    List<String> videoIds = new ArrayList<>();

    for (PlaylistItem playListItem : playlistItemList)
    {
        videoIds.add(playListItem.getContentDetails().getVideoId());
    }

    return videoIds;
}

基于 YouTube API 和 Jeremy Walker 的 sample code

【讨论】:

    猜你喜欢
    • 2012-10-28
    • 2016-08-05
    • 2015-09-02
    • 2013-09-28
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2019-09-13
    相关资源
    最近更新 更多