使用 mvc 应用程序将视频上传到 youtube(所有代码都隐藏)

本文关键字:代码 隐藏 youtube 应用程序 mvc 视频 使用 | 更新日期: 2023-09-27 18:34:55

这太疯狂了,我已经花了一个星期试图弄清楚这一点。我发现的所有内容要么已弃用,要么无法正常工作。

这就是我要做的。我们让用户上传视频,我们存储视频,直到它被批准。一旦获得批准,我们需要将其上传到我们的 YouTube 频道。

来自google:https://developers.google.com/youtube/v3/code_samples/dotnet#retrieve_my_uploads 的样本不会通过GoogleWebAuthorizationBroker.AuthorizeAsync,因为它只是永远挂起。

这种方法的另一个问题是,我们需要在上传视频后获得id,并且我们需要知道视频是否上传成功,所有这些都是同步的。 通过查看代码,您将看到它正在使用异步方法,并获取视频的 ID,有一个回调。

有没有人知道如何在 mvc 应用程序的后端同步上传视频?

使用 mvc 应用程序将视频上传到 youtube(所有代码都隐藏)

好的,所以我遇到的第一个问题是身份验证挂起(从GoogleWebAuthorizationBroker.AuthorizeAsync获取凭据(。解决此问题的方法是使用GoogleAuthorizationCodeFlow,它不是异步的,并且不会尝试在appdata文件夹中保存任何内容。

我需要获取刷新令牌,为此,我遵循:具有OAuth的Youtube API单用户场景(上传视频(

若要获取可以多次使用的刷新令牌,必须获取已安装应用程序的客户端 ID 和机密。

证书是困难的部分,在那之后一切都很好。不过有一点需要注意,因为我花了几个小时试图在上传视频时找出 CategoryId 是什么。我似乎找不到任何关于示例代码在哪里得到"22"的真正解释。我发现22是默认值,意思是"人物和博客"。

这是我为任何需要它的人编写的代码(我还需要能够删除 youtube 视频,所以我在这里添加了它(:

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[] 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 和秘密提供的项目名称,但我认为您可以放置任何东西吗?