运行下面的代码示例时,只有第一个YouTube播放列表($ YouTubePlaylistId1)会使用添加的视频($ YouTubeVideoId)进行更新。
第二个YouTube播放列表($ YouTubePlaylistId2)没有更新。
我没有收到任何错误,但是下面的json正文仍然存在问题吗?还是我在这里想念的其他东西?
我试图切换播放列表ID,但它始终是第一个定义的要更新的播放列表。
如果两个变量中设置的播放列表ID相同,则没有区别:$ YouTubePlaylistId1,$ YouTubePlaylistId2和唯一视频($ YouTubeVideoId)。
尝试了json主体($ YouTubePlaylistVideoBody)的多种变体,但通常以错误告终json格式而告终。
# Set the first yt playlist id
$YouTubePlaylistId1 = "DummyPlaylistId1"
# Set the second yt playlist id
$YouTubePlaylistId2 = "DummyPlaylistId2"
# Set the yt video id
$YouTubeVideoId = "dQw4w9WgXcQ"
# Set access token provided by https://developers.google.com/oauthplayground
$GoogleAccessToken = "DummyAccessToken"
# Set the rest method to yt playlistitems
$YouTubePlaylistVideoUri = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet"
# Set the JSON body
$YouTubePlaylistVideoBody = @"
{
"snippet": {
"playlistId": "$YouTubePlaylistId1",
"resourceId": {
"kind": "youtube#video",
"videoId": "$YouTubeVideoId"
}
},
"snippet": {
"playlistId": "$YouTubePlaylistId2",
"resourceId": {
"kind": "youtube#video",
"videoId": "$YouTubeVideoId"
}
}
}
"@
# Create new header object
$HeaderValue = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
# Append the Google access token in $GoogleAccessToken
$HeaderValue.Add("Authorization", "Bearer $GoogleAccessToken")
# Invoke rest command
Invoke-RestMethod -Headers $HeaderValue -Uri $Uri -Method "POST" -Body
$YouTubePlaylistVideoBody -ContentType "application/json"
预期结果是两个youtube播放列表都更新了视频。
实际结果是仅更新了第一个播放列表(在$ YouTubePlaylistVideoBody中定义),而没有报告任何错误。
编辑:尝试使用如下所示的两个不同的JSON数组,但它们均以错误“(400)Bad Request”结尾:
$YouTubePlaylistVideoBody = @"
{
"snippet": [
"playlistId": "$YouTubePlaylistId1",
"resourceId": {
"kind": "youtube#video",
"videoId": "$YouTubeVideoId"
},
"playlistId": "$YouTubePlaylistId2",
"resourceId": {
"kind": "youtube#video",
"videoId": "$YouTubeVideoId"
}
]
}
"@
$YouTubePlaylistVideoBody = @"
[
"snippet": {
"playlistId": "$YouTubePlaylistId1",
"resourceId": {
"kind": "youtube#video",
"videoId": "$YouTubeVideoId"
}
},
"snippet": {
"playlistId": "$YouTubePlaylistId2",
"resourceId": {
"kind": "youtube#video",
"videoId": "$YouTubeVideoId"
}
}
]
"@