【问题标题】:Trying to upload a video to YouTube, the video uploaded but I'm getting an exception after it's uploaded尝试将视频上传到 YouTube,视频已上传,但上传后出现异常
【发布时间】:2011-09-21 11:15:52
【问题描述】:

这是代码:

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 Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.YouTube;



namespace YoutubeTesting
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        YouTubeRequestSettings settings;
        YouTubeRequest request;
        string devkey = "AI39si6xhSQXx95FTYIACWPfq-lLIphblgaReuz9z6VEjR1Q6YjrV6FRN2U6FN6P6-lGF2OYaUZhCVOKJ_MCk4o6kPeUszvf5A";
        string username = "tbout72";
        string password = "password here";


        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                settings = new YouTubeRequestSettings("You Manager", devkey, username, password);
                request = new YouTubeRequest(settings);
                Video video = new Video();
                video.Title = "test";
                video.Tags.Add(new MediaCategory("Comedy", YouTubeNameTable.CategorySchema));
                video.Keywords = "Comedy";
                video.Private = false;
                video.MediaSource = new MediaFileSource("d:\\P9170031.MOV", "video/MOV");
                request.Upload(video);
                MessageBox.Show("Successfully Uploaded");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

我从http://code.google.com/p/google-gdata/downloads/list引用了一些dll文件 我下载了两个文件:Google_Data_API_Setup_1.9.0.0.msi Google Data API Setup (1.9) 和 YouTube_SDK_1.9.0.0.msi

现在我尝试再次上传视频文件,这个视频文件上传得很好,一次没有问题,视频在 youtube.com 上运行 但即使它在我的代码中工作,我也会遇到异常错误。无论上传是否正常,我都会遇到异常。

  1. 程序运行并挂了几分钟,运行时我看不到form1。
  2. 几分钟后,我得到了异常。

即使我收到异常并且每次程序运行时都会收到异常,但文件上传良好。

我不知道为什么,但我看到该文件被上传了 3-5 次,其中一两次上传得很好,其他文件被拒绝,因为它是重复文件。但是我运行了一次程序,为什么有时它会一次上传文件这么多次?现在我又试了一次;该程序不再运行,我在 10 分钟前关闭了它。并且在完成该过程后看到文件上传并正在处理我再次看到文件上传,现在它被拒绝,因为它是重复的。但是为什么程序没有运行,它一直在上传文件呢?

上传之间的时间大约是 14 分钟,我的程序关闭了同一个文件我没有再运行它,但是我看到两个同名的文件,同一个文件:一个可以,一个因重复而被拒绝。

所以我现在有 3 个问题:

  1. 运行程序时看不到form1,就像程序挂起一样。

  2. 一直出现异常错误。

  3. 文件上传不止一次。

异常上线:request.Upload(video);

异常错误信息为:执行请求失败:

https://uploads.gdata.youtube.com/feeds/api/users/default/uploads

这是完全的例外:

Google.GData.Client.GDataRequestException was caught
  Message=Execution of request failed: https://uploads.gdata.youtube.com/feeds/api/users/default/uploads
  Source=Google.GData.Client
  StackTrace:
       at Google.GData.Client.GDataRequest.Execute()
       at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
       at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
       at Google.GData.Client.GDataGAuthRequest.Execute(Int32 retryCounter)
       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 YoutubeTesting.Form1.Form1_Load(Object sender, EventArgs e) in D:\C-Sharp\Youtube_Testing\YoutubeTesting\YoutubeTesting\Form1.cs:line 47
  InnerException: System.Net.WebException
       Message=The operation has timed out
       Source=System
       StackTrace:
            at System.Net.HttpWebRequest.GetResponse()
            at Google.GData.Client.GDataRequest.Execute()
       InnerException: 

form1.cs 的第 47 行是:request.Upload(video);

现在我再次看到它上传了 4 次文件: 无法弄清楚为什么以及如何继续上传文件。 其中3个文件因重复而被拒绝,1个可以!

我问了一个人,他告诉我这对他有用,没有任何问题。

我正在使用 Visual Studio 2010 pro .net 4.0 Windows 7 64 位。

我多次检查了我的 YouTube 登录名和密码,也在我的 Gmail 中检查了相同的问题和异常。

【问题讨论】:

    标签: c# youtube-api google-data-api


    【解决方案1】:

    异常说明这里发生了什么:您的请求超时。您可以使用this property 更改超时。默认设置为 100,000 毫秒。您可以将其设置为更大的数字,以增加超时时间或设置为 -1 以保持 HTPPRequestObject 的默认值:

    settings.Timeout = -1;
    

    此外,您应该在单独的线程中上传视频。这样一来,GUI 就会保持响应,不会像您说的那样“挂起”。

    更新:

    你可以这样开始一个线程:

    new Thread(new ThreadStart(() =>
    {
        var settings = new YouTubeRequestSettings("You Manager", devkey, username, password)
        {
            Timeout = -1
        };
        var request = new YouTubeRequest(settings);
        var video = new Video()
        {
            Title = "test",
            Tags.Add(new MediaCategory("Comedy", YouTubeNameTable.CategorySchema)),
            Keywords = "Comedy",
            Private = false,
            MediaSource = new MediaFileSource("d:\\P9170031.MOV", "video/MOV"),
        };
        request.Upload(video);
    })).Start();
    

    【讨论】:

    • 你能告诉我如何在我的代码中使用线程吗?找不到方法。
    【解决方案2】:

    这里是通过 c# 上传 YouTube 视频的教程。

    参考这里:http://codewithgeek.blogspot.com/2015/12/upload-video-to-youtube-from-c-aspnet.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-03
      • 1970-01-01
      • 2012-07-19
      • 2012-03-07
      • 2014-11-02
      • 2017-08-22
      • 2014-01-26
      • 2011-11-30
      相关资源
      最近更新 更多