1

I'm trying to get history feed from YouTube of an authenticated user with python. This is my code :

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() 

and it says gdata.service.RequestError: {'status': 400, 'body': 'Invalid request URI', 'reason': 'Bad Request'} . I know I have to make a authenticated Get request , but i don't know how. What am I doing wrong ?

EDIT

I am facing a major problem. The prog is the same as above , but with yt_service.developer_key = DEVELOPER_KEY added under password line and uri = 'https://gdata.youtube.com/feeds/api/users/default/watch_history?v=2&key=%s'%DEVELOPER_KEY. I tested it in 4 PCs and it runs without errors only in one if them. I get this error :

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'}

I use python 2.7 and gdata python 2.0 . Why one Pc executes it and the rest of them not? What can i do to fix it ? Please help!

Dubniak
  • 13
  • 4

1 Answers1

1

When you attempt to call youtube API, you will first need to register a new application. Reference - https://developers.google.com/youtube/2.0/developers_guide_protocol_authentication

Visit http://code.google.com/apis/youtube/dashboard/ to register your application and retrieve the Developer Key that will be generated for you.

Thereafter, whenever you make a call to youtube APIs, you should include the key query parameter. (reference - https://developers.google.com/youtube/2.0/developers_guide_protocol#Developer_Key)

Your instantiated yt_service will be:-

yt_service.developer_key = DEVELOPER_KEY

where the DEVELOPER_KEY is the one that you get on your newly registered application's dashboard ( http://code.google.com/apis/youtube/dashboard/ ).

Without this DEVELOPER_KEY, google youtube will not know whether your python script is in fact a recognized application, with proper access rights.

Calvin Cheng
  • 35,640
  • 39
  • 116
  • 167
  • I did as you said , but i keep getting this error . I got a developer key , added it to the yt_service instance before ProgrammaticLogin() and sent a GetYouTubeVideoFeed to uri = 'https://gdata.youtube.com/feeds/api/users/default/watch_history?v=2&key=%s' %DEVELOPER_KEY , but nothing changed. – Dubniak Nov 05 '12 at 15:14
  • Still not working at my machine. It works on other machines , but not on mine. Why is that ? I use python 2.6.5 and youtube API v2.0 . – Dubniak Nov 16 '12 at 17:43