Embed a video in a playlist with youtube api v3

Hey, so I'm pretty familiar with the youtube api. I know how to read feeds and a playlist along with getting any important video information that I identify as important. I use php to do all this, but to make sure that I have the right idea of ​​how each request / response works. I tried it in a browser using an API browser

Now I am trying to insert a video into the playlist that I just created (also through the API), but I have some problems figuring out how my request is not generated correctly

Request here

POST https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&key={YOUR_API_KEY} Content-Type: application/json Authorization: Bearer ya29.1.AADtN_WT2TRZzH1t86nVlX26z9WPp-gnDTxVHGvdQ6xx0vyTzmkYeXkLdJerwllLzF_a X-JavaScript-User-Agent: Google APIs Explorer { "snippet": { "playlistId": "PL8hD12HFC-nuswc21_e64aPAy9B25sEH7", "resourceId": { "videoId": "KMGuyGY5gvY" } } } 

Here is the answer

 400 Bad Request - Show headers - { "error": { "errors": [ { "domain": "youtube.playlistItem", "reason": "invalidResourceType", "message": "Resource type not supported." } ], "code": 400, "message": "Resource type not supported." } } 

the playlist is an empty public playlist that I created using the API API and is viewable through the standard YouTube website. The video was accidentally selected from youtube ... I tried several, because I thought it might be a specific problem with the video, but not luck .. I turned on OAuth and use an account that is a verified YouTube partner.

However, I can understand why the resource type is not supported. I browsed the web along with the answers on the left, not finding what I was doing wrong. Any help with this problem would be greatly appreciated since I was kind of stuck at the moment

+8
youtube youtube-api
source share
3 answers

Your resourceId not complete since it does not identify the API how to interpret the videoId parameter. Try setting the kind resourceId attribute, for example:

  "snippet": { "playlistId": "PL8hD12HFC-nuswc21_e64aPAy9B25sEH7", "resourceId": { "kind": "youtube#video", "videoId": "KMGuyGY5gvY" } } 

This way, the API will know in which domain (so to speak) to find the resource identified by the string you are sending.

+14
source share

this will help build resourceID

 def add_video_to_playlist(videoID,playlistID): youtube = get_authenticated_service() #write it yourself add_video_request=youtube.playlistItems().insert( part="snippet", body={ 'snippet': { 'playlistId': playlistID, 'resourceId': { 'kind': 'youtube#video', 'videoId': videoID } #'position': 0 } } ).execute() 
+1
source share

Read the documentation on Youtube Data Api

  $playlistId = 'REPLACE_WITH_YOUR_PLAYLIST_ID_STARTS_WITH_PL'; $resourceId = new Google_Service_YouTube_ResourceId(); $resourceId->setVideoId('REPLACE_WITH_VIDEO_ID_YOU_WANT_TO_ADD'); $resourceId->setKind('youtube#video'); $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet(); $playlistItemSnippet->setTitle('YOUR_VIDEO_TITLE'); $playlistItemSnippet->setPlaylistId($playlistId); $playlistItemSnippet->setResourceId($resourceId); $playlistItem = new Google_Service_YouTube_PlaylistItem(); $playlistItem->setSnippet($playlistItemSnippet); $playlistItemResponse = $youtube->playlistItems->insert( 'snippet,contentDetails', $playlistItem, array());</i> 

The full project is available on my github

0
source share

All Articles