Python Youtube API:上传功能不起作用

时间:2012-12-12 17:15:45

标签: python upload youtube youtube-api

我想创建一个允许用户将视频上传到Youtube的应用程序。 这是我尝试过的代码(改编自http://code.google.com/p/gdata-python-client/source/browse/tests/gdata_tests/youtube/service_test.py):

my_media_group = gdata.media.Group(
                                       title = gdata.media.Title(text=title),
                                       description = gdata.media.Description(description_type='plain',
                                                                             text=description),
                                       keywords = gdata.media.Keywords(text= tags),
                                       category = gdata.media.Category(
                                                                       text='Autos',
                                                                       scheme='http://gdata.youtube.com/schemas/2007/categories.cat',
                                                                       label='Autos'),
                                       player=None
    )
    # Set Geo location to 37,-122 lat, long
    where = gdata.geo.Where()
    where.set_location((37.0,-122.0))
    video_entry = gdata.youtube.YouTubeVideoEntry(media=my_media_group, geo=where)
    print "Creating a video entry: done."
    print "Uploading video"
    new_entry = self.client.InsertVideoEntry(video_entry, filepath)
    print "Done uploading video."

它总是停留在倒数第二行:

new_entry = self.client.InsertVideoEntry(video_entry, filepath)

那有什么不对?是否有关于filepath的要求(例如,路径'C:\ video.avi'是否有效?)

事实上,我只需要一种上传方式,所以请提出任何可能的解决方案。

编辑:1。我尝试嵌入Youtube上传小部件,但它似乎不起作用:(只有一个选项可以从网络摄像头加载,但这也不起作用。)

<iframe id="widget" type="text/html" width="640" height="390"
  src="https://www.youtube.com/upload_embed" frameborder="0"></iframe>
  1. 我使用QWebview加载Youtube的上传页面,但Youtube始终返回错误。 QWebview: Upload to Youtube return error
  2. 编辑2:

    我将代码更改为:

    def getYtSession(self, email = '', password =''):
        # if we do not want to reuse our session.
        if email != '' and password != '':
            yt_service = gdata.youtube.service.YouTubeService()
            yt_service.email = email
            yt_service.password = password
    
            #login.
            try:
                print yt_service.ProgrammaticLogin()
                self.email = email
                self.password = password
                self.logged_in = True
                self.emit(QtCore.SIGNAL('doneLogin(QString)'), QtCore.QString(email + " " + password))
                return yt_service
            except:
                #Display a warning dialog.
                self.logged_in = False
                self.emit(QtCore.SIGNAL('failedLogin()'))
                return None      
        elif self.yt_service: # we want to reuse the session.
            return self.yt_service
        else:
            return gdata.youtube.service.YouTubeService()
    

    并在上传功能中:

    self.yt_service = self.getYtSession(self.email, self.password)
        try:
            new_entry = self.yt_service.InsertVideoEntry(video_entry, filepath)
            print "Done uploading video."
            self.emit(QtCore.SIGNAL('doneUpload(QString)'), QtCore.QString('Title: ' + title + '\nPath: ' + filepath))
        except:
            print "Stack trace:"
            traceback.print_stack()
    

    它仍然不起作用。还有一个问题:堆栈跟踪没有任何帮助:

    Stack trace:Login successfully.
    

    文件“D:\ workplace \ simple-media-player \ trunk \ MyPlayer \ src \ QtThread.py”,第19行,运行中     self.function(* self.args,** self.kwargs)   在doRealUpload中输入文件“D:\ workplace \ simple-media-player \ trunk \ MyPlayer \ src \ videoplayer.py”,第490行     traceback.print_stack()

    该消息(“登录成功。”)由发出信号doneLogin(QString)时调用的函数打印。如果我不放

    new_entry = self.yt_service.InsertVideoEntry(video_entry, filepath)
            print "Done uploading video."
            self.emit(QtCore.SIGNAL('doneUpload(QString)'), QtCore.QString('Title: ' + title + '\nPath: ' + filepath))
    

    在try-except中,然后它什么都不打印。

    更多信息:我将视频信息设置如下: 路径:C:/Users/huynh/Desktop/REcord1.wmv 名称:'测试视频' 标签:'test,youtube' 描述:fafafa

1 个答案:

答案 0 :(得分:0)

好的,没关系。我修改了http://code.google.com/p/gdata-python-client/source/browse/tests/gdata_tests/youtube/service_test.py处的代码,它完美无缺!