-
Aug 19th, 2022, 05:54 AM
#1
Thread Starter
Lively Member
How to add a Video to a specific YouTube Playlist?
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:
Code:
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Reflection
Imports System.Threading
Imports System.Threading.Tasks
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Services
Imports Google.Apis.Upload
Imports Google.Apis.Util.Store
Imports Google.Apis.YouTube.v3
Imports Google.Apis.YouTube.v3.Data
Public 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 Function
End 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 adding the video. This is the playlist ID you obtained in step 1. The snippet.resourceId.kind contains the value youtube#video. The snippet.resourceId.videoId identifies the video that you are adding to the playlist. The property value is a unique YouTube video ID.
As someone stated on some forum
You have to create a new PlaylistItemSnippet object, specify the PlayList ID, build a new ResourceId object, specify the Kind and VideoID 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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|