我有一個Python/Django的動力應用程序,它有一個特點,我可以通過使用gdata.youtube Python庫如何通過gdata.youtube Python庫上傳YouTube視頻在Django
我引用發佈到YouTube視頻如下所示:https://developers.google.com/youtube/1.0/developers_guide_python
前開發人員遵循此非常有用的頁面中的說明,例如履行要求和驗證以上傳視頻。但它不起作用。 當我運行螢火蟲時,有一個500內部服務器錯誤,它跟蹤我的視圖中的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_po st.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內部服務器錯誤,我需要在這些代碼行中找到問題。任何答案都非常感謝。
你從哪裏得到500?如果gdata返回500,它的YouTube的bug;如果你自己的代碼,你應該在這裏發佈調試跟蹤 – pinkdawn
哦,我真的嗎?它也是我的理論之一,它是一個YouTube的錯誤,但如果是這樣,我該如何得到它的工作? – SimonBASS
在video.save中對函數upload_prepare的調試跟蹤,所以錯誤很可能在forms.py中保存,但我找不到錯誤 – SimonBASS