通过YouTube API将视频添加到播放列表时请求URI无效

2 投票
1 回答
2432 浏览
提问于 2025-04-16 08:24

我在用youtube gdata python api往我的播放列表里添加视频时,遇到了一个错误,一直没法解决。

gdata.service.RequestError: {'status': 400, 'body': '无效的请求地址', 'reason': '错误的请求'}

这个链接似乎也是遇到了同样的错误,但还没有找到解决办法。大家能帮帮我吗?

 import getpass
 import gdata.youtube
 import gdata.youtube.service

 yt_service = gdata.youtube.service.YouTubeService()

 # The YouTube API does not currently support HTTPS/SSL access.
 yt_service.ssl = False
 yt_service = gdata.youtube.service.YouTubeService()
 yt_service.email = #myemail
 yt_service.password = getpass.getpass()
 yt_service.developer_key = #mykey
 yt_service.source = #text
 yt_service.client_id= #text
 yt_service.ProgrammaticLogin()

 feed = yt_service.GetYouTubePlaylistFeed(username='default')
 # iterate through the feed as you would with any other
 for entry in feed.entry:
     if (entry.title.text == "test"):
         lst = entry;
         print entry.title.text, entry.id.text

 custom_video_title = 'my test video on my test playlist'  
 custom_video_description = 'this is a test video on my test playlist'  
 video_id = 'Ncakifd_16k'  
 playlist_uri = lst.id.text    

 playlist_video_entry = yt_service.AddPlaylistVideoEntryToPlaylist(playlist_uri, video_id, custom_video_title, custom_video_description)    

 if isinstance(playlist_video_entry, gdata.youtube.YouTubePlaylistVideoEntry):
 print 'Video added'

让我困惑的是,更新播放列表是可以的,但添加视频却不行。

 playlist_entry_id = lst.id.text.split('/')[-1]
 original_playlist_description = lst.description.text
 updated_playlist = yt_service.UpdatePlaylist(playlist_entry_id,'test',original_playlist_description,playlist_private=False)

视频ID没有问题,因为我用的是示例代码里的视频。那我到底漏掉了什么呢?求助!

谢谢大家。

1 个回答

2

Gdata似乎在使用v1版本的API。所以,相关的文档在这里:http://code.google.com/apis/youtube/1.0/developers_guide_protocol.html#Retrieving_a_playlist

这意味着,你的“playlist_uri”不应该使用“lst.id.text”的值,而应该使用“feedLink”元素的“href”属性,这样才能和“AddPlaylistVideoEntryToPlaylist”一起使用。

即使你使用的是v2版本的API,你也应该按照文档的说明,从“content”元素的“src”属性中获取URI,只需在上面的链接中把2.0替换掉就可以了!(因为我的声望不够,SO不让我放两个链接!:))

撰写回答