Youtube API 获取未列出的视频 [英] Youtube API get unlisted videos

查看:26
本文介绍了Youtube API 获取未列出的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我必须列出来自 youtube 用户帐户的所有视频.我正在获取所有公共视频,但是当我在 oauth 之后进行 api 调用时,我仍然只能获取公共视频.

for a project i have to list all videos from a youtube user account. I'm getting all public videos, but when i make an api call after oauth i still get only public videos.

未列出的视频是从搜索引擎和用户公共页面隐藏的视频.我确定有办法找回它.

Unlisted videos are videos that are hidden from search engine and user public page. I'm sure there is a way to retrieve that.

这是我的代码

var request = gapi.client.youtube.playlistItems.list({
            playlistId: listId,
            part: 'snippet,status',
            maxResults: 25,
            pageToken: nextPageToken
        });

        request.execute(function(response) {
            console.log(response)
            nextPageToken = response.nextPageToken
            if ('error' in response) {
                displayMessage(response.error.message);
            } else {
                if ('items' in response) {
                    console.log(response.items)

                    var a = [];
                    for(var i in response.items){

                        var d = {
                            title: response.items[i].snippet.title,
                            videoId: response.items[i].snippet.resourceId.videoId,
                            publishedAt: response.items[i].snippet.publishedAt
                        }
                        a.push(d);

                        $("#message").append(JSON.stringify(d))
                    }



                } else {
                    displayMessage('There are no videos in your channel.');
                }
            }
        });

推荐答案

您可以在不使用 API 密钥的 OAuth 路由的情况下获取未列出的视频,尽管许多在线表示您不能.这是我发现的:

You can get unlisted videos without going the OAuth route using an API key though many online say you cannot. Here is what I found:

转到 https://console.developers.google.com 并为 Youtube 创建一个 API 密钥数据 API v3.API 密钥,而不是 OAuth ClientId.您的 API 密钥应类似于AISdkJKdk7GuSkDKJDKSkLmSSDDFm4ro4E_4et_ww"

Go to https://console.developers.google.com and create an API key for Youtube Data API v3. API key, NOT OAuth ClientId. Your API key should look something like "AISdkJKdk7GuSkDKJDKSkLmSSdDFm4ro4E_4et_ww"

如果使用 C# 从 Nuget 下载 Google.Apis.YouTube.v3 版本 1.40.2.1593.如果不下载与您的语言对应的库.

If using C# Download Google.Apis.YouTube.v3 Version 1.40.2.1593 from Nuget. If not download the equivalent library for your language.

接下来转到您的 You Tube 帐户并创建一个名为API 密钥返回的未列出的视频"的新播放列表(在 YouTube 工作室中,如果您编辑已上传的视频,则有一个播放列表的下拉菜单,您可以在其中分配一个已经存在或创建一个新的)

Next go to your You Tube account and create a new playlist called "Unlisted Videos returned by API Key" (In YouTube studio if you edit a video you already have uploaded there is a drop down menu for Playlist where you can assign one that already exists or create a new one)

然后转到您的频道,然后点击播放列表标签.编辑您刚刚创建的新播放列表,使其不公开,否则您添加到此播放列表的不公开视频将显示在您频道的用户界面上.

Then go to your channel, and click the playlist tab. Edit the new playlist you just created so that it is Unlisted or else your Unlisted videos you add to this playlist will be visible on the UI of your channel.

在您的频道上找到您再次创建的播放列表并点击查看.您被带到的 URL 将有一个 &list 查询字符串参数,您需要获取该 ID.示例:https://www.youtube.com/watch?v=kskScbSkdSDg&list=DKfiVi8sZdkZqW-saZt7bN8DDTDxshjDK.在此示例中,我们的列表 (PlalistId) 值为 DKfiVi8sZdkZqW-saZt7bN8DDTDxshjDK,在调用 GetPlaylistVideos 方法时将需要该值.

On your channel find the playlist you created again and click to view it. The URL you are taken to will have a &list query string parameter and you need to get that Id. Example: https://www.youtube.com/watch?v=kskScbSkdSDg&list=DKfiVi8sZdkZqW-saZt7bN8DDTDxshjDK. In this example our list (PlalistId) value is DKfiVi8sZdkZqW-saZt7bN8DDTDxshjDK which will be needed when calling our GetPlaylistVideos method.

现在您已经完成了先决条件.代码时间:

Now you have the pre-requisites done. Time for the code:

    public static List<YouTubeVideo> GetPlaylistVideos(string PlaylistId)
    {
        List<YouTubeVideo> result = new List<YouTubeVideo>();

        try
        {
            YouTubeService service = new YouTubeService(new BaseClientService.Initializer()
            {
                ApplicationName = YOUTUBE_APPLICATION_NAME,
                ApiKey = YOUTUBE_API_KEY
            });

            var nextPageToken = "";
            while (nextPageToken != null)
            {
                var playlistItemsListRequest = service.PlaylistItems.List("snippet");
                playlistItemsListRequest.PlaylistId = PlaylistId;
                playlistItemsListRequest.MaxResults = 50;
                playlistItemsListRequest.PageToken = nextPageToken;

                // Retrieve the list of videos uploaded to the authenticated user's channel.
                var playlistItemsListResponse = playlistItemsListRequest.Execute();

                foreach (var playlistItem in playlistItemsListResponse.Items)
                {

                    YouTubeVideo v = new YouTubeVideo
                    {
                        EmbedUrl = String.Format("https://www.youtube.com/embed/{0}", playlistItem.Snippet.ResourceId.VideoId),
                        Title = playlistItem.Snippet.Title,
                        Description = playlistItem.Snippet.Description,
                        ThumbnailUrl = playlistItem.Snippet.Thumbnails.High.Url
                    };

                    result.Add(v);
                }

                nextPageToken = playlistItemsListResponse.NextPageToken;
            }

        }

如果您调用 GetPlaylistVideos,您将看到它使用 API 密钥而不是 OAuth 从您的未列出的播放列表中返回未列出的视频.

If you call the GetPlaylistVideos you will see it returns the unlisted videos from your unlisted playlist using an API key and not OAuth.

这篇关于Youtube API 获取未列出的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆