好的,所以我使用下面的php代码试图让youtube返回用户的视频列表,但是,他们的“项目”显示为空。
$apiKey = "MY-API-KEY";
$channel_name = "UCSzDPMGFGxDgM4YtNM4arTA";
$count = 1;
$apiv3 = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=".$channel_name."&key=".$apiKey));
foreach($apiv3->items as $item):
$uploads = $item->contentDetails->relatedPlaylists->uploads;
endforeach;
$youtube = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&contentDetails&maxResults=".$count."&playlistId=".$uploads."&key=".$apiKey));
return $youtube;
这就是返回的内容:
{
"kind": "youtube#channelListResponse",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/3cxjRXf86G9z5Bg7rup3QfCgrxM\"",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": [
]
}
然而,在他们的网页上,他们上传了视频。我如何让它工作?我在其他用户上使用了完全相同的代码并且工作正常。
答案 0 :(得分:3)
您可以改用视频搜索API。将list
方法与channelId
参数一起使用。抓住这个网址:
https://www.googleapis.com/youtube/v3/search?
part=snippet&channelId={THE_CHANNEL_ID}&key={YOUR_API_KEY}
响应将如下所示(从 Try It 部分复制):
{
"kind": "youtube#searchListResponse",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/28IrYJUKXsyiTkjfJRPDVrAyaLo\"",
"nextPageToken": "CAUQAA",
"pageInfo": {
"totalResults": 9,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/u8AoS8t4dI9bLdKn4n1eMb9XQeY\"",
"id": {
"kind": "youtube#channel",
"channelId": "UCSzDPMGFGxDgM4YtNM4arTA"
},
"snippet": {
"publishedAt": "2013-03-02T00:26:17.000Z",
"channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
"title": "Iron Hulk",
"description": "",
"thumbnails": {
"default": {
"url": "https://lh4.googleusercontent.com/-JeG2zOT2Eu8/AAAAAAAAAAI/AAAAAAAAAAA/9XlpZULoZ8E/photo.jpg"
},
"medium": {
"url": "https://lh4.googleusercontent.com/-JeG2zOT2Eu8/AAAAAAAAAAI/AAAAAAAAAAA/9XlpZULoZ8E/photo.jpg"
},
"high": {
"url": "https://lh4.googleusercontent.com/-JeG2zOT2Eu8/AAAAAAAAAAI/AAAAAAAAAAA/9XlpZULoZ8E/photo.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/sfhJOiYZgSFY3vR7MTCQqT5gR8c\"",
"id": {
"kind": "youtube#video",
"videoId": "pqJGSSbII6E"
},
"snippet": {
"publishedAt": "2014-01-02T06:00:06.000Z",
"channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
"title": "Iron Hulk Fitness Group Sessions Compilation Jan 2014",
"description": "A compilation of a few of the group sessions conducted in late 2013. Visit our facebook page for more information.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/pqJGSSbII6E/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/pqJGSSbII6E/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/pqJGSSbII6E/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/r91n5PeMndZnvf10n6T_L2dJYGg\"",
"id": {
"kind": "youtube#video",
"videoId": "I8bpjJqTnRI"
},
"snippet": {
"publishedAt": "2013-10-27T05:55:13.000Z",
"channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
"title": "Iron Hulk Advanced plyometric bodyweight circuit 27/10/13",
"description": "This weeks advanced class circuit focusing on plyometric and explosive movements 3 rounds for time: 12 x plyometric alternating hands push ups 8 x Alternating ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/I8bpjJqTnRI/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/I8bpjJqTnRI/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/I8bpjJqTnRI/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/xrF0LOpaWmqPl4E2isJ7r4VL2Ds\"",
"id": {
"kind": "youtube#video",
"videoId": "Y3bK24QHlxk"
},
"snippet": {
"publishedAt": "2014-11-15T19:07:35.000Z",
"channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
"title": "PORC Nov 2014",
"description": "Iron Hulk's first PORC event. November 15th 2014.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/Y3bK24QHlxk/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/Y3bK24QHlxk/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/Y3bK24QHlxk/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/WQudUOFtaQNk4DyhMg7-Wx2tTAE\"",
"id": {
"kind": "youtube#video",
"videoId": "TAgZL5OzVsU"
},
"snippet": {
"publishedAt": "2015-05-05T21:12:20.000Z",
"channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
"title": "IHF 2015",
"description": "What we've been up to in the first half of 2015.",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/TAgZL5OzVsU/default.jpg"
},
"medium": {
"url": "https://i.ytimg.com/vi/TAgZL5OzVsU/mqdefault.jpg"
},
"high": {
"url": "https://i.ytimg.com/vi/TAgZL5OzVsU/hqdefault.jpg"
}
},
"channelTitle": "",
"liveBroadcastContent": "none"
}
}
]
}
答案 1 :(得分:1)
我相信部分属性应该以逗号分隔。
$youtube = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults...));
答案 2 :(得分:1)
@SalmanA有一个答案,使用可以工作的搜索终点;但是,如果您希望使用上传播放列表,它也可以进行一些调整(只是通过API资源管理器确认)。首先,正如@SauminiNavaratnam所提到的,您的“part”参数需要以逗号分隔。其次,在检索通道详细信息时,无法使用forUsername参数传递通道ID。您需要使用forUsername参数传入用户名,或者将通道ID作为id参数传递(请注意,您遇到的问题是channelList响应为空,并且因此没有什么可以传递给playlistItems调用)。如果您进行了这些更改,您将获得正确的上传ID,然后您的playlistItems调用将获得与搜索调用相同的结果。
然而,当完成所有操作后,您可能更喜欢搜索端点,因为它可以按日期,受欢迎程度等订购结果。