c# - 为什么尝试将视频文件上传到 youtube 时出现 InvalidCredentialsException?

标签 c# .net youtube youtube-api google-client-login

这是我在 form1 中的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;


namespace YouTube_Manager
{
    public partial class Form1 : Form
    {

        YouTubeRequestSettings settings;
        YouTubeRequest request;

        string username = "myusername", password = "mypass", devkey = "mydevkey";
        string filename, filetype, filemime;

        public string Devkey
        {
            get { return devkey; }
            set { devkey = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public string Username
        {
            get { return username; }
            set { username = value; }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Devkey = devkey;
            Password = password;
            Username = username;
            settings = new YouTubeRequestSettings("YouTube_Manager", devkey, username, password);
            request = new YouTubeRequest(settings);
            if (cmbCat.Items.Count > 0)
            {
                cmbCat.SelectedIndex = 0;
            }
            if (cmbPrivacy.Items.Count > 0)
            {
                cmbPrivacy.SelectedIndex = 0;
            }
        }


        public void UploadVideo()
        {
            Video video = new Video();
            video.Title = txtTitle.Text;
            video.Tags.Add(new MediaCategory(cmbCat.SelectedItem.ToString(), YouTubeNameTable.CategorySchema));
            video.Keywords = txtKeyWords.Text;
            if (cmbPrivacy.SelectedIndex == 1)
                video.Private = true;
            else
                video.Private = false;
            GetFileMime();
            video.MediaSource = new MediaFileSource(filename, filemime);
            request.Upload(video);
            MessageBox.Show("Successfully uploaded");
        }

        public void GetFileMime()
        {
            switch (filetype)
            {
                case "flv": filemime = "video/x-flv"; break;
                case "avi": filemime = "video/avi"; break;
                case "3gp": filemime = "video/3gpp"; break;
                case "mov": filemime = "video/quicktime"; break;
                default: filemime = "video/quicktime"; break;
            }
        }

        private void btnChoosefile_Click(object sender, EventArgs e)
        {
            string tmp;
            choosefile.ShowDialog();
            tmp = choosefile.FileName;
            txtFilepath.Text = tmp;
            string[] title = tmp.Split('\\');
            int i = title.GetUpperBound(0);
            string temp = title[i];
            string[] title1 = temp.Split('.');
            txtTitle.Text = title1[0];
            filename = tmp.Replace("\\", "\\\\");
            filetype = title1[1];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("By clicking 'upload,' " +
            "you certify that you own all rights to the content or that you are authorized" +
            "by the owner to make the content publicly available on YouTube, and that it otherwise" +
            "complies with the YouTube Terms of Service located at http://www.youtube.com/t/terms", "Aggrement",
            MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                UploadVideo();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

异常就行了:
request.Upload(video);

在谷歌开发站点中,我创建了一个名为 Youtube Uploader 的应用程序。
我在我的 c# 应用程序中使用的关键是客户端 ID 以:apps.googleusercontent.com 结尾

但我不确定这是正确的 key 。
至于用户名,我使用的是我用来登录 youtube 的 gmail 电子邮件。
还有密码。

在我的代码中 cmbCat 和 cmbPrivacy 是组合框。

这是异常消息:
Google.GData.Client.InvalidCredentialsException was unhandled
  HResult=-2146233088
  Message=Invalid credentials
  Source=Google.GData.Client
  StackTrace:
       at Google.GData.Client.Utilities.QueryClientLoginToken(GDataCredentials gc, String serviceName, String applicationName, Boolean fUseKeepAlive, IWebProxy proxyServer, Uri clientLoginHandler)
       at Google.GData.Client.GDataGAuthRequest.QueryAuthToken(GDataCredentials gc)
       at Google.GData.Client.GDataGAuthRequest.EnsureCredentials()
       at Google.GData.Client.GDataRequest.EnsureWebRequest()
       at Google.GData.Client.GDataGAuthRequest.EnsureWebRequest()
       at Google.GData.Client.GDataGAuthRequest.CopyRequestData()
       at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
       at Google.GData.Client.GDataGAuthRequest.Execute()
       at Google.GData.Client.MediaService.EntrySend(Uri feedUri, AtomBase baseEntry, GDataRequestType type, AsyncSendData data)
       at Google.GData.Client.Service.Insert(Uri feedUri, AtomEntry newEntry, AsyncSendData data)
       at Google.GData.Client.Service.Insert[TEntry](Uri feedUri, TEntry entry)
       at Google.GData.YouTube.YouTubeService.Upload(String userName, YouTubeEntry entry)
       at Google.YouTube.YouTubeRequest.Upload(String userName, Video v)
       at Google.YouTube.YouTubeRequest.Upload(Video v)
       at YouTube_Manager.Form1.UploadVideo() in d:\C-Sharp\YouTube_Manager\YouTube_Manager\YouTube_Manager\Form1.cs:line 81
       at YouTube_Manager.Form1.button1_Click(Object sender, EventArgs e) in d:\C-Sharp\YouTube_Manager\YouTube_Manager\YouTube_Manager\Form1.cs:line 122
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at YouTube_Manager.Program.Main() in d:\C-Sharp\YouTube_Manager\YouTube_Manager\YouTube_Manager\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

最佳答案

ClientLogin (登录名和密码)已于 2012 年 4 月 20 日弃用,并于 2015 年 5 月 26 日关闭。此代码将不再有效,您需要切换到使用 Oauth2。

我还想建议您使用新的 Google 客户端库

Install-Package Google.Apis.YouTube.v3

关于c# - 为什么尝试将视频文件上传到 youtube 时出现 InvalidCredentialsException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30835055/

相关文章:

c# - 如何动态生成必须放置在动态生成的组框内的切换按钮

c# - 使用 .NET 和 C# 的 MongoDB 代码片段

youtube - 在哪里可以找到 'youtube-v3-api-captions.json'发现文档?

android - 在 Android 应用程序中嵌入 Youtube 视频

c# - 匹配以特定字符开头的字符串

c# - 同一属性上的初始化 + 私有(private)集访问器?

.net - 为什么 Windows\Assembly 中可能缺少 System.Threading.dll?

javascript - 我正在制作一个 Discord 机器人,需要将一些 Powershell 代码翻译成 Javascript

C#:匿名变量的内存分配

c# - vs201 0 auto 化: Get the text value of a EnvDTE. CodeElement