我有一个python / django支持的应用程序,它有一个功能,我可以使用gdata.youtube python库将视频发布到youtube
我的参考如下:https://developers.google.com/youtube/1.0/developers_guide_python
前开发人员按照这个非常有用的页面中的说明进行操作,例如满足上传视频的要求和身份验证。但它不起作用。 当我运行firebug时,有一个500 INTERNAL SERVER ERROR,它跟踪upload_prepare 我的视图中的功能,很可能是在保存部分。
def upload_prepare(request):
if request.POST:
form = UploadForm(request.user, request.POST)
if form.is_valid():
#error is in the next line
video = form.save()
他还按照有关如何通过Browserupload
发布YouTube视频的说明进行操作我的代码如下:
models.py
class Category(models.Model):
text = models.CharField(max_length=100)
label = models.CharField(max_length=100)
scheme = models.URLField()
def toGData(self):
return gdata.media.Category(
text=self.text,
label=self.label,
scheme=self.scheme)
class YoutubeUser(models.Model):
username = models.CharField(max_length=50)
...
#Initialize gdata.youtube.service.YouTubeService object
def yt_service(self):
try:
return selt._yt_service
except AttributeError:
#setting up developer key and client ID
yt_service = gdata.youtube.service.YouTubeService()
yt_service.developer_key = settings.SYNCR_YT_DEVKEY
yt_service.client_id = 'LOLtest'
#setting up a authentication token
if self.token:
yt_service.SetAuthSubToken(self.token)
self._yt_service = yt_service
return yt_service
#Setting up AuthSub authentication for web apps
def authsub_url(self, request):
#urls.py url
base = reverse('authlanding', args=(self.id,))
next = 'http://%s%s?next=%s' % (request.get_host(), base, urllib2.quote(request.build_absolute_uri()))
scope = 'http://gdata.youtube.com'
secure = False
session = True
return self.yt_service.GenerateAuthSubUrl(next, scope, secure, session)
#upgrade to a session token
def upgrade_to_session(self):
yt_service = self.yt_service
yt_service.SetAuthSubToken(self.token)
yt_service.UpgradeToSessionToken()
self.token = yt_service.current_token.get_token_string()
self.save()
到目前为止,我的models.py中的代码,这里是我的forms.py中的代码,导致错误的保存函数在这里找到(查看上面的upload_prepare视图)
forms.py
class UploadForm(forms.Form):
video_title = forms.CharField(max_length=100)
...
#https://developers.google.com/youtube/1.0/developers_guide_python#UploadingVideos
#Browser-based upload
def save(self):
mediagroup = gdata.media.Group(
title=gdata.media.Title(text=self.cleaned_data['video_title']),
description=gdata.media.Description(description_type="plain", text=self.cleaned_data['video_description']),
keywords=gdata.media.Keywords(text=self.cleaned_data['tags']),
#category already in GData format look in models.py
category=self.cleaned_data['category'].toGData(),
player=None,
)
video_entry = gdata.youtube.YouTubeVideoEntry(media=mediagroup)
response = self.yt_user.yt_service.GetFormUploadToken(video_entry)
post_url = response[0]
youtube_token = response[1]
video = Video.objects.create(
author=self.yt_user,
post_url=post_url,
youtube_token=youtube_token,
title=self.cleaned_data['video_title'],
description=self.cleaned_data['video_description'],
tag_list=self.cleaned_data['tags'],
)
self.video = video
return video
另一方面,html中用于传递数据的表单结合了django表单,django自定义标记和ajax
forms.py
class UploadForm(forms.Form):
video_title = forms.CharField(max_length=100)
video_description = forms.CharField(widget=forms.Textarea(attrs={'cols': 60,
'rows': 10}))
category = forms.ModelChoiceField(Category.objects, empty_label=None)
tags = TagField(
label=_('tags (comma separated)'),
required=True,
)
youtube_tags.py
def youtube_upload_form(context):
#UploadForm from above
upload_form = UploadForm(context['user'])
context['upload_form'] = upload_form
return context
register.inclusion_tag('syncr/youtube/upload.html',
takes_context=True)(youtube_upload_form)
add_post.html
<script>
function upload_video_to_youtube(url){
action = "%s?nexturl=%s" % (youtube_url, url)
var new_form = create_element('form', {'method': 'post', 'action': action, 'enctype':"multipart/form-data"});
$('#youtube_upload_form input').each(function(){
var obj = this.cloneNode(true);
new_form.appendChild(obj);
});
document.body.appendChild(new_form);
new_form.submit();
}
</script>
{% block body %}
...
<div id="rd2-radioOptionContent" class="hide radioOptionContent">
{% youtube_upload_form %}
</div>
{% endblock %}
希望每个人都能清楚地了解我发布的代码。如果阅读清楚,大多数指令发布在参考文献中(虽然用于提交数据的表格略有不同),并且鉴于我有500内部服务器错误,我需要在这些代码行中找到问题。任何答案都非常感谢。
答案 0 :(得分:0)
你的yt_service方法的第二行有一个拼写错误,应该是self not selt。也许这就是问题所在?