1

There is this doc. available. So I used

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());
}

Just to get 401 unauthorized. What do I need to change. If you ask, all sources I found are either outdated or people were not dealing with that issue.

For "Appname","devkey" I used the appropriate values aswell as for pw and username.

Zurechtweiser
  • 1,165
  • 2
  • 16
  • 29
  • are you missing the clientId parameter in `YouTubeRequestSettings` or does this method have an overload? – Adam Sweeney May 25 '12 at 17:46
  • Google.YouTube.YouTubeRequestSettings does not contain a constructor that takes 5 arguments, which would include clientid. Actually no constructor includes clientid anymore. I read it is deprecated. – Zurechtweiser May 25 '12 at 18:17
  • it needs clientId parameter. http://google-gdata.googlecode.com/svn-history/r902/docs/folder56/M_Google_YouTube_YouTubeRequestSettings__ctor_2.htm – prashanth May 25 '12 at 18:41
  • 1
    It does not take the clientID. Try it out, you'll see. – Zurechtweiser May 25 '12 at 19:00

1 Answers1

4

I'm afraid in this case, as expected with a 401 unauthorized error, you must be giving incorrect details. I went to the trouble to try your code and it worked as expected, and uploaded the video. Your devkey, pw or username must be incorrect, or there must be a problem outside of the code posted above, since it worked fine for me.

However, you should really use a background worker for this task, perhaps like this:

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...");
        }
    }
}

Hope you sort it out!

  • One addition: When trying to comment to a video by replacing with " var request = new YouTubeRequest(settings); var newVideo = new Video(); newVideo.VideoId = "59y0rPq79CM"; Comment c = new Comment(); c.Content = "Interesting"; try { request.AddComment(newVideo, c); } catch (Exception fff) { MessageBox.Show(fff.ToString()); throw; }" the comment does not show up. Ideas? – Zurechtweiser May 25 '12 at 19:48
  • according to https://stackoverflow.com/questions/30306509/upload-video-to-youtube-using-a-simple-asp-net-application-and-oauth-2-0 this api is depcircated.. :( – SHEKHAR SHETE Jun 17 '17 at 05:43