C# Youtube API v3.NET集成
我的应用程序中集成了youtube api v2。 我有API密钥,我想集成Youtube API v3,但我得到了错误401(响应状态代码不表示成功) 在 Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)位于Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(任务任务任务)位于Google.api.Upload.ResumableUpload`1.d_u0.MoveNext(),位于c:\code\Google.com\Google api dotnet client\default\Tools\Google.api.Release\bin\Debug\output\default\Src\GoogleApis\api[Media]\Upload\ResumableUpload.cs:第377行 这是相关代码:C# Youtube API v3.NET集成,c#,youtube,youtube-api,C#,Youtube,Youtube Api,我的应用程序中集成了youtube api v2。 我有API密钥,我想集成Youtube API v3,但我得到了错误401(响应状态代码不表示成功) 在 Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)位于Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(任务任务任务)位于Google.api.Upload.Resumab
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "myapikey",
ApplicationName = ConfigurationManager.AppSettings["YoutubeApplicationName"].ToString(),
});
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"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
var filePath = @"REPLACE_ME.mp4"; // Replace with path to actual movie file.
using (streamVideo)
{
var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", streamVideo, "video/*");
videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
await videosInsertRequest.UploadAsync();
}
将错误401列出为authorizationRequired
或youtubeSignupRequired
(在请求上下文错误标题下)。我不确定如何确定完整的错误信息——如果可以从异常中看到实际的HTTP响应
将代码与比较,您似乎没有将HttpClientInitializer
值与YouTube用户的凭据一起传递。YouTube将需要这个来确定视频将上传到哪个频道
相关片段:
UserCredential credential;
using (var stream = new FileStream("client_secrets.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
});
我找到了解决办法。代码不起作用,因为应用程序托管在IIS上。IIS访问%appdata%文件夹内的文件夹时可能出现问题。默认情况下,令牌文件保存在%appdata%文件夹中。我试图更改对该文件夹的权限。我添加了所有人、DefaultAppPool、I_用户,。。。但是没有成功。还有更新版本的Google.api(1.8.2),它允许完整路径(
newfiledatastore(fullPath,true)
)true
表示完整路径,但即使在那里也有bug。不可能使用该构造函数,因为我总是遇到错误。所以我决定我的应用程序应该使用IIS Express(事实上,我创建了一个新项目,里面有授权代码)。转到解决方案属性并设置应用程序使用IIS Express
- 从(选择web应用程序)获取cliendId(json文件),并为javascript源设置
(或应用程序端口使用的任何端口),为重定向url设置http://localhost:48372
。您还应该在internet上找到如何使端口静态)。保存你的clientId文件。(这一步您可能已经完成了)http://localhost:48372/authorize.aspx
- 在使用IIS express时,AuthorizeAsync方法应该没有问题。系统将提示您输入您的凭据并选择您的youtube帐户(如果您已经登录,则只需选择帐户)。当您授予应用权限时,您将被重定向到authorize.aspx。授权aspx页面应使用提供的客户端机密属性和数据存储属性创建新的IAuthorization流对象。使用
从URL读取代码,并创建code=Request.Params[“code”]
对象。UserCredential
属性的类型为YouTube服务的HttpClientInitializer
。您应该使用的方法类似于var usercredential=UserCredential
。之后,包含刷新令牌的文件将在指定的flow.SomeMethodWhichReceives(“user”、code、CancelletionToken.None)
文件夹中生成李>FileDataStore
- 当您拥有刷新令牌时,您可以做任何您想做的事情,因为刷新令牌应该永远持续(除非手动撤销)。有一种方法允许从refreshtoken获取usercredential,类似于
flow.getAccessTokenfromRefreshToken(“用户”,“您的\u刷新\u令牌\u字符串”)
UserCredential
编辑:还有一个我忘记提及的问题(对于那些认为这就是全部的人)。当您授予应用程序访问google api的权限时(就在您被reddirected授权.aspx之前),您将得到错误重定向\u url不匹配,因为url参数
重定向\u url
中的某个地方与您在ClientId json中提供的重定向url不匹配(至少出于某种原因,端口不同)。更改此参数,使其与clientId json中提供的重定向url相匹配。是的,我必须使用HttpClientInitializer,但当我调用GoogleWebAuthorizationBroker.AuthorizationAsync时,什么都没有发生。程序挂起,没有例外,任何事情。