Youtube api 上传 video.net

Youtube api upload video.net

我正在尝试创建一个网站,您可以从该网站将视频从您的设备上传到 youtube,但能够查看您从我的网站上传的那些特定视频。(它还有更多用途)

我正在使用 .net c#,我有我的 oauth 密钥和开发人员 api 密钥,下载了 json 文件等。我下载了示例代码 google 有这个但不能似乎可以正常工作,但不完全确定它是如何工作的。

有人可以帮我解决这个问题/向我解释一下吗?

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;

/// <summary>
/// Summary description for Video_Upload
/// </summary>
public class Video_Upload
{
    public Video_Upload()
    {
    //
    // TODO: Add constructor logic here

     new Video_Upload().Run().Wait();

}

private async Task Run()
{
    UserCredential credential;
    using (var stream = new FileStream("Client_id_googleApi.json",           FileMode.Open, FileAccess.Read))
    {
        credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream).Secrets,
            // This OAuth 2.0 access scope allows an application to upload files to the
            // authenticated user's YouTube channel, but doesn't allow other types of access.
            new[] { YouTubeService.Scope.YoutubeUpload },
            "user",
            CancellationToken.None
        );
    }


    var youtubeService = new YouTubeService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
    });

    //VIDEO INFO AND DETAILS

    var video = new Video();
    video.Snippet = new VideoSnippet();
    video.Snippet.Title = "Test Video 1";
    video.Snippet.Description = "Testing Video Upload";
    video.Snippet.Tags = new string[] { "Test", "First" };
    video.Snippet.CategoryId = "17";//category id for sport // See https://developers.google.com/youtube/v3/docs/videoCategories/list
    video.Snippet.ChannelId = "UCfvR-wqeoHmAGrHnoQRfs9w";
    video.Status = new VideoStatus();
    video.Status.PrivacyStatus = "public"; // or "private" or "public"
    var filePath = @"C:\Users\siobhan\Documents\Visual Studio 2015\WebSites\FYP_November\GP010149.avi"; // Replace with path to actual movie file.

    using (var fileStream = new FileStream(filePath, FileMode.Open))
    {
        var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
        videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
        videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

        await videosInsertRequest.UploadAsync();
    }
    void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
        switch (progress.Status)
        {
            case UploadStatus.Uploading:
                Console.WriteLine(progress.BytesSent, " bytes sent.");
                break;

            case UploadStatus.Failed:
                Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
                break;
        }
    }
    void videosInsertRequest_ResponseReceived(Video video)
{
        Console.WriteLine("Video id '{0}' was successfully uploaded.",         video.Id);
    }


    }



}

It says 'void cannot be used in this way'

因为您正试图在另一个方法中声明一个方法:

private async Task Run()
{
    //...

    void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
    {
        //...
    }

    //...
}

这在 C# 中无效。方法在 class 内部声明,并且基本上彼此并排。如:

private async Task Run()
{
    //...
}

void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
    //...
}