视频上传至YouTube与MVC应用程序(后面的所有代码) [英] Upload video to youtube with mvc application (all code behind)

查看:345
本文介绍了视频上传至YouTube与MVC应用程序(后面的所有代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是真是疯了,我已经花了一个星期,现在试图弄清楚这一点。一切,我觉得要么已过时或只是不工作。

This is just crazy, i've spent a week now trying to figure this out. Everything i find is either deprecated or will just not work.

这就是我想要做的事。我们有用户上传的视频中,我们存储的视频,直到它已被批准。 。一旦批准,我们需要把它上传到我们的YouTube频道

So this is what i'm trying to do. We have users upload a video, we store the video until it has been approved. once it is approved, we need to upload it to our youtube channel.

从谷歌示例:的 https://developers.google.com/youtube/v3/code_samples/dotnet#retrieve_my_uploads 不会得到过去的GoogleWebAuthorizationBroker.AuthorizeAsync,因为它只是挂起,直到永远。

The sample from google: https://developers.google.com/youtube/v3/code_samples/dotnet#retrieve_my_uploads will not get past the GoogleWebAuthorizationBroker.AuthorizeAsync because it just hangs forever.

这种方法的另一个问题是,我们需要的ID的视频上传后,我们需要知道,如果影片成功与否上传,全部同步。你会通过在代码中查找使用异步方法,并获取视频有回调的id看看。

Another problem with this approach is that we need the id after the video is uploaded, and we need to know if the video was uploaded successfully or not, all in sync. you'll see by looking at the code it is using async methods and to get the id of the video there is a callback.

没有任何人有任何想法如何上传在同步MVC应用程序的后端视频?

Does anybody have any idea how to upload a video in the back end of an mvc application synchronously?

推荐答案

好了,第一个问题,我是在认证被挂(从GoogleWebAuthorizationBroker.AuthorizeAsync获得凭证)。解决这个问题的方法是使用GoogleAuthorizationCodeFlow,这是不异步,并且不尝试保存AppData文件夹东西。

Ok, so the first problem i had was the authentication was hanging (getting credentials from GoogleWebAuthorizationBroker.AuthorizeAsync). The way around this was to use GoogleAuthorizationCodeFlow, which is not async, and does not try to save anything in the appdata folder.

我需要得到一个刷新令牌,要做到这一点我也跟着:
使用OAuth(上传视频)

I needed to get a refresh token, and to do that i followed: Youtube API single-user scenario with OAuth (uploading videos)

要获取可使用多次刷新令牌,你必须去获得安装应用程序客户端ID和密码。

To get a refresh token that can be used many times, you have to go to get a client id and secret for an INSTALLED APPLICATION.

的凭据是艰难的部分,在那之后事情就好了。有一点要注意,虽然,因为我花了几个小时,试图找出什么是类别ID上传视频时。我似乎无法找到在哪里示例代码得到了22任何真正的解释。我发现22是默认设置,意味着人们&安培;博客

The credentials was the tough part, after that things were just fine. One thing to note though, because i spent a couple hours trying to find out what CategoryId was when uploading a video. I couldn't seem to find any real explanation on where the sample code got "22". I found 22 was the default, and meant "People & Blogs".

我的继承人对任何人的代码需要它(我还需要能够删除YouTube视频,所以我补充说,在这里):

Heres my code for anybody that needs it (I also needed to be able to delete youtube videos, so i've added that in here to):

public class YouTubeUtilities
{
    /*
     Instructions to get refresh token:
     * http://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[] tags, String categoryId, Boolean isPublic)
    {
        var video = new Video();
        video.Snippet = new VideoSnippet();
        video.Snippet.Title = title;
        video.Snippet.Description = desc;
        video.Snippet.Tags = tags;
        video.Snippet.CategoryId = categoryId; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = isPublic ? "public" : "private"; // "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;
    }

    public void DeleteVideo(String videoId)
    {
        var videoDeleteRequest = youtube.Videos.Delete(videoId);
        videoDeleteRequest.Execute();
    }

    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;
        }
    }
}



我还没有尝试过,但是从我的理解ApplicatioName可以任何你想要的。我只是测试,这是该项目的名字我在YouTube上的客户端ID和秘密,但我认为你可以把任何东西?

I haven't tried it, but from what i understand ApplicatioName can be whatever you want. I was just testing, and this is the project name i have in youtube for the client id and secret, but i think you can just put anything?

这篇关于视频上传至YouTube与MVC应用程序(后面的所有代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆