Trouble migrating from V2 to V3 of YouTube API - c

2019-02-26 01:36发布

First off, apologize to post this, I don't think it is a duplicate since I kept crawling YouTube API tagged questions since yesterday and I couldn't find a fix.

My Question is, Can I keep using V2 of YouTube API even if it is officially deprecated ? the reason why is because I have 2 issues I still couldn't find a fix for them in V3 and I already have them in V2 : listing a channel's last uploads and popular uploads.

For the last uploads, in V2 I use http://gdata.youtube.com/feeds/api/users/youtube/uploads?prettyprint=true , while I add ?orderby=viewCount to list the popular ones. I couldn't find an alt in V3 [ Tough I tried https://www.googleapis.com/youtube/v3/videos?part=snippet&chart=mostPopular&forUsername={Username}&maxResults=5&key={key} but it keeps listing videos from a channel I visited yesterday even if I change the username, making me think there is problem with the link or my key, a server key by the way ]

Any ideas ?

Thanks a lot !

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-26 02:13

Rather than using the videos->list endpoint, you should instead use the search endpoint, like this:

https://www.googleapis.com/youtube/v3/search?order=viewCount&part=snippet&channelId=UCuo5NTU3pmtPejmlzjCgwdw&maxResults=25&key={YOUR_API_KEY}

You can do order=date to have them ordered chronologically rather than by popularity if you'd rather. There are also a lot of other parameters you can use ... see https://developers.google.com/youtube/v3/docs/search/list for more details.

If you don't know the channel IDs, but only the usernames, you'll have to have multiple calls ... a call to this endpoint:

https://www.googleapis.com/youtube/v3/channels?part=snippet&forUsername={username}&key={YOUR_API_KEY}

This will return a packet with the channel ID in it. If you'd like to avoid making multiple calls or storing the username/channelID on your own, you could do the video search with the 'q' parameter set to be the username. It often works but you can get extraneous channels that way.

查看更多
登录 后发表回答