Why when trying to upload a video file to youtube i'm getting InvalidCredentialsException?

207 views Asked by At

This is my code in 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();
        }
    }
}

The exception is on the line:

request.Upload(video);

In the google development site i created application called it Youtube Uploader. And the key i'm using in my c# app is the Client Id ending with: apps.googleusercontent.com

But i'm not sure this is the right key. As for a user name i'm using my gmail email that i use to log in to youtube. And the password.

In my code cmbCat and cmbPrivacy are comboBoxes.

This is the exception message:

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

There are 1 answers

0
Linda Lawton - DaImTo On BEST ANSWER

ClientLogin (login and password) was deprecated as of April 20, 2012 and turned off on May 26 2015. This code will not longer work you need to switch to using Oauth2.

I would like to also recomed you use the new Google client library

Install-Package Google.Apis.YouTube.v3