为什么在尝试将视频文件上传到YouTube时,我得到无效凭据异常

本文关键字:无效 异常 YouTube 为什么 视频 文件 | 更新日期: 2023-09-27 17:56:04

这是我在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上传器的应用程序。我在 c# 应用程序中使用的密钥是以以下结尾的客户端 ID:apps.googleusercontent.com

但我不确定这是正确的钥匙。至于用户名,我正在使用用于登录YouTube的Gmail电子邮件。和密码。

在我的代码中,cmbCat和cmbPrivacy是comboBoxes。

这是异常消息:

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:

为什么在尝试将视频文件上传到YouTube时,我得到无效凭据异常

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

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

Install-Package Google.Apis.YouTube.v3