How to upload Video to youtube using Google.Apis.YouTube.v3 and C#?

后端 未结 3 1977
慢半拍i
慢半拍i 2021-01-01 19:39

I have created console application using C#. Which will upload Video from local drive to youtube. I have cr

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 20:27

    All Credit to @iedoc for his code and answer here

    A basic working Web project example

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    
    
    
    
        testing
    
    
        

    Default.aspx.cs

    using System;
    using System.IO;
    using System.Reflection;
    using System.Threading;
    using System.Threading.Tasks;
    
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Services;
    using Google.Apis.Upload;
    using Google.Apis.Util.Store;
    using Google.Apis.YouTube.v3;
    using Google.Apis.YouTube.v3.Data;
    using Google.Apis.Auth.OAuth2.Flows;
    using Google.Apis.Auth.OAuth2.Responses;
    
    public partial class _Default : System.Web.UI.Page
    {
        string vID = "none";
        public void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void saveDetails_click(object sender, EventArgs e)
        {
            if (Path.GetFileName(videoUpload.PostedFile.FileName) != "")
            {
                YouTubeUtilities ytU = new YouTubeUtilities("REFRESH", "SECRET", "CLIENT_ID"); // pass in your API codes here
    
                using (var fileStream = videoUpload.PostedFile.InputStream) // the selected post file
                {
                    vID = ytU.UploadVideo(fileStream,videoName.Text,videoDesc.Text,"22",false);
                }
                Response.Write(vID);
            }
    
        }
    }
    public class YouTubeUtilities
    {
        /*
         Instructions to get refresh token:
         * https://stackoverflow.com/questions/5850287/youtube-api-single-user-scenario-with-oauth-uploading-videos/8876027#8876027
         * 
         * When getting client_id and client_secret, use installed application, other (this will make the token a long term token)
         */
        private String CLIENT_ID { get; set; }
        private String CLIENT_SECRET { get; set; }
        private String REFRESH_TOKEN { get; set; }
    
        private String UploadedVideoId { get; set; }
    
        private YouTubeService youtube;
    
        public YouTubeUtilities(String refresh_token, String client_secret, String client_id)
        {
            CLIENT_ID = client_id;
            CLIENT_SECRET = client_secret;
            REFRESH_TOKEN = refresh_token;
    
            youtube = BuildService();
        }
    
        private YouTubeService BuildService()
        {
            ClientSecrets secrets = new ClientSecrets()
            {
                ClientId = CLIENT_ID,
                ClientSecret = CLIENT_SECRET
            };
    
            var token = new TokenResponse { RefreshToken = REFRESH_TOKEN };
            var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
                new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = secrets
                }),
                "user",
                token);
    
            var service = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credentials,
                ApplicationName = "TestProject"
            });
    
            //service.HttpClient.Timeout = TimeSpan.FromSeconds(360); // Choose a timeout to your liking
            return service;
        }
    
        public String UploadVideo(Stream stream, String title, String desc, String categoryId, Boolean isPublic)
        {
            var video = new Video();
            video.Snippet = new VideoSnippet();
            video.Snippet.Title = title;
            video.Snippet.Description = desc;
            video.Snippet.CategoryId = categoryId; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
            video.Status = new VideoStatus();
            video.Status.PrivacyStatus = isPublic ? "public" : "unlisted"; // "private" or "public" or unlisted
    
            //var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", stream, "video/*");
            var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", stream, "video/*");
            videosInsertRequest.ProgressChanged += insertRequest_ProgressChanged;
            videosInsertRequest.ResponseReceived += insertRequest_ResponseReceived;
    
            videosInsertRequest.Upload();
    
            return UploadedVideoId;
        }
    
        void insertRequest_ResponseReceived(Video video)
        {
            UploadedVideoId = video.Id;
            // video.ID gives you the ID of the Youtube video.
            // you can access the video from
            // http://www.youtube.com/watch?v={video.ID}
        }
    
        void insertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
        {
            // You can handle several status messages here.
            switch (progress.Status)
            {
                case UploadStatus.Failed:
                    UploadedVideoId = "FAILED";
                    break;
                case UploadStatus.Completed:
                    break;
                default:
                    break;
            }
        }
    }
    

    I hate .NET and I have been fighting with this for weeks. Some of the problems I encountered;

    1. a faulty pair of API Keys, they just didn't work. I guess it was a random bug.
    2. I also have a couple MP4's that I downloaded from Youtube that would not upload back, within creator studio they would say "Upload failed: Can't process file" I determined this by attempting to upload them in the youTube interface. (not through API)
    3. async vs. synchronous was causing me many issues I just don't understand.

    I would like to improve this code to provide actual upload status/feedback but I think that would need to be done client side. I am not very good at C#/.NET

    UPDATE here is my feedback code via server & client side - https://stackoverflow.com/a/49516167/3790921

提交回复
热议问题