How to use the Youtube api login procedure using c#?

不打扰是莪最后的温柔 提交于 2019-12-31 04:33:04

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/10759032/how-to-use-the-youtube-api-login-procedure-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!