如何使用C#中使用YouTube API登录程序? [英] How to use the Youtube api login procedure using c#?

查看:395
本文介绍了如何使用C#中使用YouTube API登录程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



只是为了得到401未经授权。什么我需要改变。如果你问,我发现所有的来源要么是过时的或人们不应对这个问题。

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.

有关APPNAME,devkey我用适当的值藏汉作为为PW和用户名。

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

推荐答案

恐怕在这种情况下,与一个401错误擅自预期,你必须给不正确的细节。我去了麻烦,试试你的代码和它的工作预期,并且上传了视频。您devkey,PW或用户名必须是不正确的,或者必须有上面贴的,因为它为我工作得很好代码之外的问题。

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!

这篇关于如何使用C#中使用YouTube API登录程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆