Reputation: 5471
Ok, so I've used the following php code to try to get youtube to return a users list of videos, however, their "items" is coming up as empty.
$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;
And this is what is being returned:
{
"kind": "youtube#channelListResponse",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/3cxjRXf86G9z5Bg7rup3QfCgrxM\"",
"pageInfo": {
"totalResults": 0,
"resultsPerPage": 5
},
"items": [
]
}
Yet on their page, they have uploaded videos. How do I get this to work? I've used the exact same code on another user and it works fine.
Upvotes: 2
Views: 2339
Reputation: 13667
@SalmanA has an answer that uses the search endpoint which will work; however, if you want you use the Uploads playlist, it too will work with a couple of tweaks (just confirmed it with the API explorer). First of all, as @SauminiNavaratnam mentioned, your "part" parameter needs to be comma delimited. Secondly, when you are retrieving the channel details, you cannot pass the channel ID in with the forUsername parameter. You either need to pass in the user name with the forUsername parameter, or pass the channel ID in as the id parameter instead (note that the problem you're getting is that the channelList response is empty, and hence there's nothing to pass on to the playlistItems call). If you make those changes, you'll get the right uploads ID, and then your playlistItems call will get back the same results as a search call.
However, when all is said and done you may prefer the search endpoint anyway, as it has the ability to order your results by date, by popularity, etc.
Upvotes: 1
Reputation: 8850
I believe the part attributes should be comma separated.
$youtube = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults...));
Upvotes: 1
Reputation: 272106
You can use the video search API instead. Use the list
method with channelId
parameter. Grab this URL:
https://www.googleapis.com/youtube/v3/search?
part=snippet&channelId={THE_CHANNEL_ID}&key={YOUR_API_KEY}
The response will look something like this (copied from the Try It section):
{
"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"
}
}
]
}
Upvotes: 3