【发布时间】:2014-09-10 21:19:51
【问题描述】:
我正在尝试使用 Google.Apis.YouTube.v3 客户端库 1.8.1.1110 从 .Net 4.5 C# 控制台应用程序将 av 视频上传到 YouTube。我创建了一个服务帐户,下载了一个 P12 密钥文件并启用了 YouTube Data API v3。
这是我对任何 Google API 的第一次尝试,我可能会错过明显的内容。
我收到以下错误;
YouTube Data API: Upload Video
==============================
An error prevented the upload from completing.
System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).
ved Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
ved Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
ved Google.Apis.Upload.ResumableUpload`1.<UploadAsync>d__0.MoveNext() i c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis\Apis\[Media]\Upload\ResumableUpload.cs:linje 0
An error prevented the upload from completing.
System.ArgumentNullException: Verdien kan ikke være null.
Parameternavn: baseUri
ved Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
ved Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
ved Google.Apis.Upload.ResumableUpload`1.<UploadCoreAsync>d__e.MoveNext() i c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis\Apis\[Media]\Upload\ResumableUpload.cs:linje 459
我的代码;
private async Task Run()
{
String serviceAccountEmail = "xxx@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { YouTubeService.Scope.YoutubeUpload }
}.FromCertificate(certificate));
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
var video = new Google.Apis.YouTube.v3.Data.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 = "unlisted"; // or "private" or "public"
var filePath = @"E:\OneDrive\CidWebApps\video.mp4";
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();
}
}
问候 乔恩·伊瓦尔
【问题讨论】:
标签: c# video google-oauth youtube-data-api