11

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.

Here is my code

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.');
                }
            }
        });
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Bounews
  • 121
  • 1
  • 1
  • 5
  • 3
    Are you trying to get unlisted videos for your own channel or someone else's? You can't get unlisted videos from a channel you don't own. – not_a_bot Sep 08 '15 at 18:04
  • i'm tryin to get someone else's unlisted, i'm pretty sure i can't get em, but if there is any workaround i have to try – Bounews Sep 09 '15 at 12:17
  • 1
    I don't think you can get someone else's unlisted videos through the API, only if they gave you the link to the video. Check out [this](http://stackoverflow.com/a/19322403/4241842) answer. – not_a_bot Sep 09 '15 at 16:00
  • thx i think also it is vain to try to find solutions – Bounews Sep 09 '15 at 16:51
  • Well it seems like if i call the api with the playlistId it shows me unlisted videos. For example this playlist has several public and unlisted videos https://www.youtube.com/watch?v=BoN_n57o6Pg&list=PLi0Upjez5R1T0Ihtp41Q2uctKDHhDnC_d, when i try it here https://developers.google.com/youtube/v3/docs/playlistItems/list#try-it and on my server, i get the unlisted videos whithout oauth. Weird – Bounews Sep 10 '15 at 11:55

3 Answers3

9

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:

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"

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

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.

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

        }

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

vvvv4d
  • 3,881
  • 1
  • 14
  • 18
  • 1
    Very helpful! Have you found a way to reference a video in the playlist after [listing playlist items](https://developers.google.com/youtube/v3/docs/playlistItems/list)? e.g. get the captions of the video. The [resourceId](https://developers.google.com/youtube/v3/docs/playlistItems#resource) returned does not appear to work with methods that accept videoId. – Raisin Jun 24 '22 at 01:01
  • @owen you are trying to use the resourceId that is returned and pass it to other yt API calls? I have not tried that, my use case was only to embed the video on a web page. – vvvv4d Jun 29 '22 at 20:18
  • That's correct. I created a separate question [here](https://stackoverflow.com/questions/72751205/can-youtube-api-retrieve-captions-of-an-unlisted-video?noredirect=1#comment128505539_72751205), maybe my question is not the most relevant in this thread. – Raisin Jun 30 '22 at 15:12
3

It might seem a bit counterintuitive at times, but YouTube has a quirk that when an unlisted video is placed in a playlist, it will actually be visible in that playlist both via the API and on the YouTube website itself; but only on the playlist view. The video will still not be searchable via normal means (API or website), will not show up on the 'videos' tab of a channel, and will not appear in the user's uploads playlist when exposed via the API using playlistItems.list.

grahamd
  • 31
  • 2
3

You can only get the logged in user's unlisted videos by using forMine=true and type=video parameters. The login method has to be Oauth 2.

Joey.Lu
  • 125
  • 8