Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C从youtube观看视频#_C#_Youtube - Fatal编程技术网

C# 使用C从youtube观看视频#

C# 使用C从youtube观看视频#,c#,youtube,C#,Youtube,为了将视频上传到youtube,我使用以下代码,但在这里我将指定文件的路径 private void button1_Click(object sender, EventArgs e) { string user, pass, source; user = textBox1.Text; pass = textBox2.Text; source = textBox3.Text;

为了将视频上传到youtube,我使用以下代码,但在这里我将指定文件的路径

private void button1_Click(object sender, EventArgs e)
{
            string user, pass, source;
            user = textBox1.Text;
            pass = textBox2.Text;
            source = textBox3.Text;
            YouTubeRequestSettings settings = new YouTubeRequestSettings("Eclipse", "AI39si554yyVqQDqtZff_kuRyQLg5wp5RjxCtEB_qRmsoF6Cm0AQWG-h_uuCjrmoDAZ3-32JXpAgdSOsJN7VmQzR_2CLHd7NxA", user, pass);
            YouTubeRequest request = new YouTubeRequest(settings);
            Video newVideo = new Video();
        newVideo.Title = "A";// "Smideo Generated Movie";
        newVideo.Tags.Add(new MediaCategory("Travel", YouTubeNameTable.CategorySchema));
        newVideo.Keywords = "Animals";
        newVideo.Description = "TEsting";
        newVideo.YouTubeEntry.Private = false;
        newVideo.Tags.Add(new MediaCategory("Test, Example",YouTubeNameTable.DeveloperTagSchema));
        try
        {
              request.Upload(newVideo);
        }
        catch(InvalidCredentialsException c)
        {
            MessageBox.Show("Error");
        }
}
最好创建一个进行上载的方法,而不是将所有代码放在事件处理程序中

要将视频上传到试管中,您可以使用以下代码:

public bool Upload(string title, string description, Catagory catagory,
              string keywords, string videoFileName, out string error)
{
    bool result = false;
    error = null;

    // Build byte arrays for the header file and footer
    byte[] header = Encoding.UTF8.GetBytes(GetHeader(title, description, catagory, keywords, videoFileName));
    byte[] file = File.ReadAllBytes(videoFileName);
    byte[] footer = Encoding.UTF8.GetBytes(lineTerm + boundary + "--");

    // Combine the byte arrays into one big byte array
    byte[] data = new byte[header.Length + file.Length + footer.Length];
    Array.Copy(header, 0, data, 0, header.Length);
    Array.Copy(file, 0, data, header.Length, file.Length);
    Array.Copy(footer, 0, data, header.Length + file.Length, footer.Length);

    // Using a HttpWebRequest here because it allows us to control the timeout
    HttpWebRequest req = (HttpWebRequest)WebRequest
        .Create(string.Format("http://uploads.gdata.youtube.com/feeds/api/users/{0}/uploads",
                username));
    req.Method = "POST";
    req.ContentType = string.Format("multipart/related; boundary={0};", boundaryheader);
    req.ContentLength = data.Length;
    req.Timeout = timeout;
    req.Headers.Add("Authorization", "GoogleLogin auth=" + authCode);
    req.Headers.Add("X-GData-Client", clientCode); // supposed to be optional
    req.Headers.Add("X-GData-Key", devCode);
    req.Headers.Add("Slug", Path.GetFileName(videoFileName));

    using (Stream postStream = req.GetRequestStream())
    {
        // Send the data to the server
        postStream.WriteTimeout = timeout;
        postStream.Write(data, 0, data.Length);
        postStream.Close();

        try
        {
            // Get the response back from the server
            WebResponse webResponse = req.GetResponse();
            using (Stream responseStream = webResponse.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    // Should check the response here
                    reader.Close();
                    result = true;
                }

                responseStream.Close();
            }

            webResponse.Close();
        }
        catch (WebException ex)
        {
            // Got a bad response
            using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream()))
            {
                error = sr.ReadToEnd();
            }
        }
    }

    return result;
}
更多信息,请访问:

您可以使用以下属性:

MediaFileSource ms = new MediaFileSource("C:\\test.wmv", "video/x-ms-wmv");
newVideo.YouTubeEntry.MediaSource = ms;

只需使用Google示例开始:
MediaFileSource ms = new MediaFileSource("C:\\test.wmv", "video/x-ms-wmv");
newVideo.YouTubeEntry.MediaSource = ms;