gpt4 book ai didi

c# - 带有 OAuth2 的 Youtube API v3 返回 "Received verification code. Closing..."

转载 作者:行者123 更新时间:2023-11-30 21:59:38 25 4
gpt4 key购买 nike

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

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

这是 UploadVideo 类:

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

它运行正常,然后打开一个浏览器窗口询问许可,如下所示:

OAuth2 Autorization

问题是我确认后他在浏览器中返回这个信息:

Return from OAuth2

那么,我有两个问题。这条消息是什么意思?

第二个是,在那之后,应该发生什么?因为,视频没有上传,调试也没有继续...

最佳答案

我不熟悉 C#,但我对 OAuth 2.0 授权码授予有一些基础知识。我做了一个网络序列图,可以帮助你。

在您分享的第一个屏幕截图中,URI 包含一个带有回调 URL 的 redirect_uri 查询参数。此请求得到一个响应,HTTP 302 重定向到带有 code=... 查询参数的回调 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"
}

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

enter image description here

关于c# - 带有 OAuth2 的 Youtube API v3 返回 "Received verification code. Closing...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29151050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com