我正在尝试使用以下代码在YouTube中使用C#Win应用程序上传视频:

    public Form1()
    {
        InitializeComponent();

        Console.WriteLine("YouTube Data API: Upload Video");
        Console.WriteLine("==============================");

        try
        {
            new UploadVideo().Run().Wait();
        }
        catch (AggregateException ex)
        {
            foreach (var e in ex.InnerExceptions)
            {
                //Console.WriteLine("Error: " + e.Message);
            }
        }

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }


这是上载视频类:

internal class UploadVideo
{
    public async Task Run()
    {
        UserCredential credential;
        using (var stream = new FileStream(@"C:\Users\23679\Downloads\client_secret.json", FileMode.Open, FileAccess.Read))
        {
            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] { YouTubeService.Scope.YoutubeUpload },
                "user",
                CancellationToken.None
            );
        }

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

        var video = new Video();
        video.Snippet = new VideoSnippet();
        video.Snippet.Title = "Default Video Title";
        video.Snippet.Description = "Default Video Description";
        video.Snippet.Tags = new string[] { "tag1", "tag2" };
        video.Snippet.CategoryId = "22";
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = "private";
        var filePath = @"C:\Users\23679\Downloads\spacetestSMALL.wmv";

        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("{0} bytes sent.", progress.BytesSent);
                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);
    }


运行正常,然后打开浏览器窗口,要求许可,如下所示:



问题是我确认后,他在浏览器中返回了此消息:



所以,我有两个问题。此消息是什么意思?

第二个是,在那之后,应该怎么办?因为视频未上传,调试无法继续...

最佳答案

我不熟悉C#,但是我对OAuth 2.0授权代码授予有一些基本知识。我制作了一个网络时序图,可以为您提供帮助。

在您共享的第一个屏幕截图中,URI包含带有回调URL的redirect_uri查询参数。该请求将使用code=...查询参数通过HTTP 302重定向到回调uri来获得响应。您的应用程序应处理此请求,并将此code交换为access_token

我的假设是,您可以找到C#库来帮助您处理这些重定向和Callas,以便接收access_tokenrefresh_token,例如RFC

来自OAuth 2.0兼容服务器的响应:

HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=SplxlOBeZQQ&state=xyz


本地应用程序应提出以下要求:

 POST /token HTTP/1.1
 Host: server.example.com
 Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
 Content-Type: application/x-www-form-urlencoded

 grant_type=authorization_code&code=SplxlOBeZQQ
 &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb


来自OAuth 2.0兼容服务器的响应:

 HTTP/1.1 200 OK
 Content-Type: application/json;charset=UTF-8
 Cache-Control: no-store
 Pragma: no-cache

 {
   "access_token":"2YotnFZFEjr1zCsicMWpAA",
   "token_type":"example",
   "expires_in":3600,
   "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
   "example_parameter":"example_value"
 }


我制作的这个Web序列图可能是一个很好的解释。

关于c# - 带有OAuth2的Youtube API v3返回“收到的验证码。结束中……”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29151050/

10-15 09:47