Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

How to add a Video to a specific YouTube Playlist?

$
0
0

I'm using YouTube Data Api v3 in order to retrieve the first searched video id by typing a title into a textbox.I'm doing it with this code:

Imports SystemImports System.Collections.GenericImports System.IOImports System.ReflectionImports System.ThreadingImports System.Threading.TasksImports Google.Apis.Auth.OAuth2Imports Google.Apis.ServicesImports Google.Apis.UploadImports Google.Apis.Util.StoreImports Google.Apis.YouTube.v3Imports Google.Apis.YouTube.v3.DataPublic Class Form1    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Try            Await Run()        Catch ex As AggregateException            For Each inner In ex.InnerExceptions                MsgBox("Error: " & inner.Message)            Next        End Try    End Sub    Private Async Function Run() As Task        Dim youtubeService = New YouTubeService(New BaseClientService.Initializer() With {            .ApiKey = "API KEY",            .ApplicationName = Me.[GetType]().ToString()        })        Dim searchListRequest = youtubeService.Search.List("snippet")        searchListRequest.Q = TextBox1.Text        searchListRequest.MaxResults = 1        Dim searchListResponse = Await searchListRequest.ExecuteAsync()        Dim videos As List(Of String) = New List(Of String)()        Dim channels As List(Of String) = New List(Of String)()        Dim playlists As List(Of String) = New List(Of String)()        For Each searchResult In searchListResponse.Items            videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId))        Next        MsgBox(String.Format("Videos:" & vbLf & "{0}" & vbLf, String.Join(vbLf, videos)))    End FunctionEnd Class

I now need to add that video into a known playlist through the end point playlistItems.insert which got a request body which is playlistItem resource that specifies at least the following values:

The snippet.playlistId identifies the playlist to which you are addingthe video. This is the playlist ID you obtained in step 1. Thesnippet.resourceId.kind contains the value youtube#video. Thesnippet.resourceId.videoId identifies the video that you are adding tothe playlist. The property value is a unique YouTube video ID.

As the user @Jimi specified in here

You have to create a new PlaylistItemSnippet object, specify thePlayList ID, build a new ResourceId object, specify the Kind andVideoID and your ResourceID, add the RousourceId to the snippet([snippet].setResourceId()? no sure), then[playlistItem].setSnippet([your snippet]). Create an insert request(should be dim request =youtubeService.playlistItems().insert("snippet", [the playlist item]))and dim result = request.execute()

but my problem is I am not able to reproduce it into code and make it work together with the code above that retrieve the video id.Can someone give me an hint? Thanks


Viewing all articles
Browse latest Browse all 3831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>