观看历史记录饲料gdata python

时间:2012-11-05 13:51:26

标签: python youtube history gdata gdata-python-client

我正在尝试使用python从经过身份验证的用户的YouTube获取历史记录。 这是我的代码:

yt_service = gdata.youtube.service.YouTubeService()
def LogIn():
  login_name = raw_input('Email:')
  login_pass = getpass.getpass()
  try:
    yt_service.email = login_name
    yt_service.password = login_pass
    yt_service.ProgrammaticLogin()
  except:
    print 'False username or password. Unable to authenticate.'
    exit();

def GetHistoryFeed():
  uri = 'https://gdata.youtube.com/feeds/api/users/default/watch_history?v=2'
  feed = yt_service.GetYouTubeVideoFeed(uri)
  #PrintVideoFeed(yt_service.GetYouTubeVideoFeed(uri),'history')

LogIn()

GetHistoryFeed() 

它说gdata.service.RequestError:{'status':400,'body':'无效的请求URI','原因':'错误请求'}。我知道我必须进行经过身份验证的Get请求,但我不知道如何。我究竟做错了什么 ?

编辑

我正面临一个重大问题。 prog与上面相同,但在密码行和yt_service.developer_key = DEVELOPER_KEY下添加了uri = 'https://gdata.youtube.com/feeds/api/users/default/watch_history?v=2&key=%s'%DEVELOPER_KEY。我在4台PC上进行了测试,如果它们只运行一台,它运行时没有错误。我收到这个错误:

File "/usr/local/lib/python2.6/dist-packages/gdata/youtube/service.py", line 186, in return self.Get(uri, converter=gdata.youtube.YouTubeVideoFeedFromString) File "/usr/local/lib/python2.6/dist-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'}

我使用python 2.7和gdata python 2.0。为什么一个Pc执行它而其余的不执行?我该怎么办才能解决这个问题?请帮忙!

1 个答案:

答案 0 :(得分:1)

当您尝试调用youtube API时,首先需要注册一个新的应用程序。参考 - https://developers.google.com/youtube/2.0/developers_guide_protocol_authentication

访问http://code.google.com/apis/youtube/dashboard/注册您的应用程序并检索将为您生成的开发人员密钥。

此后,每当您调用youtube API时,都应包含key查询参数。 (参考 - https://developers.google.com/youtube/2.0/developers_guide_protocol#Developer_Key

您实例化的yt_service将是: -

yt_service.developer_key = DEVELOPER_KEY

DEVELOPER_KEY是您在新注册的应用程序的信息中心(http://code.google.com/apis/youtube/dashboard/)上获得的那个。

如果没有此DEVELOPER_KEY,google youtube将无法知道您的python脚本实际上是否为具有适当访问权限的已识别应用程序。

相关问题