如何使用 C# 的 Youtube api 登录过程



有这个文档可用。所以我用

YouTubeRequestSettings settings = new YouTubeRequestSettings("Appname","devkey", textBox1.Text, textBox2.Text);
request = new YouTubeRequest(settings);
Video newVideo = new Video();
newVideo.Title = "Test";
newVideo.Tags.Add(new MediaCategory("Animals", YouTubeNameTable.CategorySchema));
newVideo.Description = "Testing Testing Testing";
newVideo.YouTubeEntry.Private = false;
newVideo.YouTubeEntry.MediaSource = new MediaFileSource("C:\BabyBoyScenesBackground_PAL.wmv", "video/x-ms-wmv");
try
{
  request.Upload(newVideo);
}
catch (Exception ccc)
{
  MessageBox.Show(ccc.ToString());
}

只是为了获得未经授权的401。我需要改变什么。如果你问,我找到的所有来源要么已经过时,要么人们没有处理这个问题。

对于"Appname","devkey",我使用了适当的值以及pw和用户名。

怕在这种情况下,正如预期的那样,出现 401 未经授权的错误,您必须提供不正确的详细信息。我麻烦地尝试了您的代码,它按预期工作,并上传了视频。您的开发密钥、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...");
        }
    }
}

希望你能解决它!

相关内容

  • 没有找到相关文章

最新更新