YouTube Data API playlistItems始终返回400错误。

huangapple go评论76阅读模式
英文:

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?

英文:

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?

答案1

得分: 0

我找到了需要添加的内容!我忘记将搜索参数 "part" 设置为 "snippet",下面是更新后的代码:

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"]
            }
        }
    }
})
英文:

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"]
            }
        }
    }
})

huangapple
  • 本文由 发表于 2023年5月15日 00:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定