Python Youtube API:尝试访问稍后观看结果时URI无效

0 投票
1 回答
905 浏览
提问于 2025-04-17 18:04

相信问题已经解决:Python的API只支持版本1,而“稍后观看”这个功能是在版本2中添加的。来源
解决办法:使用“实验性”的API版本3


我正在尝试使用Youtube的API来访问我的“稍后观看”播放列表。下面是我正在使用的代码。

import gdata.youtube
import gdata.youtube.service

yt_service = gdata.youtube.service.YouTubeService()
yt_service.ssl = True
yt_service.developer_key = 'REDACTED'
yt_service.email = 'REDACTED'
yt_service.password = 'REDACTED'
yt_service.ProgrammaticLogin()

playlist_uri = 'https://gdata.youtube.com/feeds/api/users/default/watch_later?v=2'
playlist_video_feed = yt_service.GetYouTubePlaylistVideoFeed(uri=playlist_uri)
for playlist_video_entry in playlist_video_feed.entry:
  print playlist_video_entry.title.text

我收到了以下错误信息。

Traceback (most recent call last):
  File "Youtube.py", line 21, in <module>
    playlist_video_feed = yt_service.GetYouTubePlaylistVideoFeed(uri=playlist_uri)
  File "/Library/Python/2.6/site-packages/gdata/youtube/service.py", line 393, in GetYouTubePlaylistVideoFeed
    uri, converter=gdata.youtube.YouTubePlaylistVideoFeedFromString)
  File "/Library/Python/2.6/site-packages/gdata/service.py", line 1108, in Get
    'reason': server_response.reason, 'body': result_body}
gdata.service.RequestError: {'status': 400, 'body': 'Invalid request URI', 'reason': 'Bad Request'}

看起来这个链接 https://gdata.youtube.com/feeds/api/users/default/watch_later?v=2 是无效的。不过,这个链接在谷歌的文档中是被提到的。我是不是用错了,还是这里有其他问题呢?

另外,如果我把链接改成 http://gdata.youtube.com/feeds/api/playlists/63F0C78739B09958,那就能正常工作。

1 个回答

0

你需要检查一下你的身份验证。根据获取和更新用户的“稍后观看”播放列表的说明:

只有在以下任一条件满足时,链接才会出现在用户的个人资料中:

你提交了一个经过身份验证的请求,以获取已登录用户自己的个人资料。

你获取的用户的“稍后观看”播放列表是公开可用的。

如果你尝试获取“稍后观看”播放列表,但以上两个条件都不满足,API服务器会返回一个40x的HTTP响应代码。

第二个链接之所以能用,很可能是因为满足了第二个公开可用的条件。我注意到你示例中缺少一个东西,就是客户端ID/来源:

# A complete client login request
yt_service.email = 'jo@gmail.com'
yt_service.password = 'mypassword'
yt_service.source = 'my-example-application'
yt_service.developer_key = 'ABC123...'
yt_service.client_id = 'my-example-application'
yt_service.ProgrammaticLogin()

你应该检查一下,确保你的身份验证过程是正确的。

撰写回答