gpt4 book ai didi

c# - .NET - 如何使用 OAuth2.0 和 Windows 服务的离线访问将视频上传到 YouTube

转载 作者:太空狗 更新时间:2023-10-30 01:29:37 24 4
gpt4 key购买 nike

我使用的是从 Nuget 下载的 Youtube .Net 库。

我创建了一个 Windows 服务,它正在检查一个文件夹以将新视频上传到 youtube。我使用控制台应用程序进行了测试,对于这种情况,用户必须在网络浏览器上手动进行授权,之后我可以毫无问题地将视频上传到 youtube。问题是当我尝试使用离线访问时。

当我在 Windows 服务上运行我的代码时,我无法从用户那里获得手动授权,所以我按照这个答案创建了我的访问 token :https://stackoverflow.com/a/20689423/2277059

问题是我没有找到任何使用独立版本的代码示例,所以我有点迷路了。使用我当前的代码,我一直在尝试上传视频时遇到错误:“unauthorized_client”。您可以在我的代码“client_secrets.json”中看到的 JSON 文件是您在为 YouTube API V3 创建凭据时自动生成的文件。

我也试过这个:https://stackoverflow.com/a/43895931/2277059 , 但得到了同样的错误。

现在我没有从代码中刷新 token ,只是在 OAuth Playground 上进行刷新并测试服务。一旦成功,我将研究如何使用每个查询刷新 token 。

创建凭据时,我选择了“其他”类型,因为这是一个 Windows 服务。

是我的代码有问题还是我在配置上遗漏了什么?

这是我的代码:

var token = new TokenResponse()
{
AccessToken = "werwdsgfdg...",
ExpiresInSeconds = 3600,
RefreshToken = "3/dsdfsf...",
TokenType = "Bearer"
};

Log.Info("Generating user credentials and secrets");

UserCredential credential;
string credentialsPath = System.AppDomain.CurrentDomain.BaseDirectory + "client_secrets.json";

using (var stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read))
{
credential = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = GoogleClientSecrets.Load(stream).Secrets,
Scopes = new[] { YouTubeService.Scope.YoutubeUpload }
}
), EsSettings.YoutubeUser, token);
}


Log.Info("Generating youtube service");
//GoogleAuthorizationCodeFlow
YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});


Log.Info("Uploading...");
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();
}

最佳答案

Error:"unauthorized_client"

表示您发送的客户端 ID 尚未经过用户身份验证。

你得到这个的原因是因为你正在做这个

Right now I'm not refreshing the token from code, just doin it on the OAuth Playground and testing the service. Once it worked I'll research about how refresh the token with every query.

token 与客户端相关联。您的 client_secrets.json 文件包含您的客户端,但您从 oauth playground 中获取的 token 与该客户端无关。

您应该在自己的机器上运行一次代码以验证它。检查可在您的 %appData% 文件夹中找到的凭据文件以获取访问 token 和刷新 token ,然后将它们发送到您的 token 。

这是文件应该包含的内容。

{  
"access_token":"ya29.4wEk2VsbqiPfR4oH5WaYo7aYgAmlP2KSIl-heyDnPBBHMYYKnfU6YuQ-_RsDofD8QR1T",
"token_type":"Bearer",
"expires_in":3600,
"refresh_token":"1/PSvxzxGB-3XU8bF2SrG6llzO-ZizE4mftrd9Edqbubg",
"Issued":"2015-09-03T11:43:47.681+02:00"
}

您可以在此处阅读有关文件创建方式的信息 FileDatastore

关于c# - .NET - 如何使用 OAuth2.0 和 Windows 服务的离线访问将视频上传到 YouTube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50908481/

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