恐怕在这种情况下,正如预期的 401 未授权错误,您必须提供不正确的详细信息。我不厌其烦地尝试了您的代码,它按预期工作,并上传了视频。您的 devkey、pw 或用户名一定不正确,或者上面发布的代码之外一定有问题,因为它对我来说很好用。
但是,您应该真正使用后台工作人员来执行此任务,可能像这样:
namespace YouTube
{
using System;
using System.ComponentModel;
using System.Windows;
using Google.GData.Client;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.YouTube;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private static BackgroundWorker uploader;
private static YouTubeRequestSettings settings;
static void UploaderDoWork(object sender, DoWorkEventArgs e)
{
var request = new YouTubeRequest(settings);
var newVideo = new Video { Title = "Test" };
newVideo.Tags.Add(new MediaCategory("Animals", YouTubeNameTable.CategorySchema));
newVideo.Description = "Testing Testing Testing";
newVideo.YouTubeEntry.Private = true;
newVideo.YouTubeEntry.MediaSource = new MediaFileSource("C:\\Wildlife.wmv", "video/x-ms-wmv");
try
{
request.Upload(newVideo);
}
catch (Exception exception)
{
MessageBox.Show("Upload failed: " + exception.Message);
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
settings = new YouTubeRequestSettings(
"app",
"devkey",
"email",
"password");
uploader = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
uploader.DoWork += UploaderDoWork;
uploader.RunWorkerCompleted += delegate { MessageBox.Show("Upload completed!"); };
uploader.RunWorkerAsync();
MessageBox.Show("Initiated upload...");
}
}
}
希望你能解决!