为什么在尝试将视频文件上传到youtube时我收到了InvalidCredentialsException?

时间:2015-06-14 22:09:36

标签: 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#app中使用的密钥是客户端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:

1 个答案:

答案 0 :(得分:1)

ClientLogin(登录名和密码)已于2012年4月20日弃用,并于2015年5月26日关闭。此代码将无法再使用Oauth2进行切换。

我还想重新使用新的Google客户端库

Install-Package Google.Apis.YouTube.v3

相关问题