Search code examples
node.jsgoogle-apiyoutubeyoutube-apiyoutube-data-api

YouTube Data API playlistItems always returns 400


I'm trying to add videos to a playlist the user entered from Node.JS, however, I always get this 400 code error:

{
    "error":{
        "code":400,
        "message":"'snippet'",
        "errors":[
            {
                "message":"'snippet'",
                "domain":"youtube.part",
                "reason":"unexpectedPart",
                "location":"part",
                "locationType":"parameter"
            }
        ]
    }
}

Here's the part of the code that adds the video to the playlist:

request.post({
    url: 'https://youtube.googleapis.com/youtube/v3/playlistItems?' + querystring.stringify({
        key: google_api
    }),
    headers: {
        'Authorization': 'Bearer ' + tokens.access_token,
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    json: {
        "snippet": {
            "playlistId": storedsessions[state]["yt-playlist"], // looks something like "PLk970BwgU6MkZpRtCnQqO43FHd7g7m_mn"
            "resourceId": {
                "kind": "youtube#video",
                "videoId": json["items"][0]["id"]["videoId"] // looks something like "aHCtj4UTQgE"
            }
        }
    }
})

Also, ignore the fact I'm using a deprecated module, I have to use it for another API I'm using and couldn't find much information on how to move to Axios or another better module (plus, it works perfectly, so I don't mind it); anyways, what can I do?


Solution

  • Found out what I needed to add! I forgot to set the search paramater "part" to "snippet", here's the updated code now:

    request.post({
        url: 'https://youtube.googleapis.com/youtube/v3/playlistItems?' + querystring.stringify({
            part: "snippet",
            key: google_api
        }),
        headers: {
            'Authorization': 'Bearer ' + tokens.access_token,
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        json: {
            "snippet": {
                "playlistId": storedsessions[state]["yt-playlist"],
                "resourceId": {
                    "kind": "youtube#video",
                    "videoId": json["items"][0]["id"]["videoId"]
                }
            }
        }
    })