c# - Youtube API v3 .NET 集成

标签 c# youtube youtube-api

我的应用程序中集成了 youtube api v2。
我有 API key ,我想集成 Youtube API v3,但我收到错误 401(响应状态码不表示成功)


Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) at Google.Apis.Upload.ResumableUpload`1.d__0.MoveNext() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis\Apis[Media]\Upload\ResumableUpload.cs:line 377



这是相关代码:
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();
}

最佳答案

YouTube API error reference将错误 401 列为 authorizationRequiredyoutubeSignupRequired (在请求上下文错误标题下)。我不确定您将如何确定完整的错误信息 - 如果可以从异常中看到实际的 HTTP 响应。

将您的代码与 sample code 进行比较,您似乎没有传递 HttpClientInitializer YouTube 用户凭据的值。 YouTube 将需要此信息来确定要将视频上传到哪个 channel 。

相关片段:

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

关于c# - Youtube API v3 .NET 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24859497/

相关文章:

jquery - 在网页上显示youtube json供稿

flash - YouTube api音频不会停止播放

c# - 调用不带参数的方法的 IL 代码

video - YouTube高等教育版的付费支持选项

arrays - 将带有嵌套数组和对象的 JSON 数据解码为 Go 结构

jquery - 将youtube视频添加到网络应用

javascript - 如何在现有的 iframe 上使用 YouTube API?

c# - TextureBrush 内存不足异常

c# - Convert.ToInt32 - 保持前导零

c# - 在浏览器中运行 Windows 窗体