尝试将视频上传到我的YouTube Channels 时出现以下错误:
System.ArgumentException: Precondition failed.: !string.IsNullOrEmpty(authorization.RefreshToken) 位于Microsoft.Runtime.CompilerServices的Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务)中的Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务),位于Microsoft.Runtime.CompilerServices的Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务) . 在c:\ code.google.com \ google-api-dotnet-client \ default_3 \ Tools \ Google.Apis.Release \ bin \ Debug \ output \ default \ Src \ GoogleApis \ Apis中ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() at Google.Apis.Upload.ResumableUpload
1.d__0.MoveNext() [Media] \ Upload \ ResumableUpload.cs:第356行
The Code:
class Program
{
private const string SERVICE_ACCOUNT_EMAIL = "685082793570-acspgovnpo2dfmcb5fsqdu5e0q7pdmn1@developer.gserviceaccount.com";
private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"97b6020dc5777485250124e25b788e4b8ed3324e-privatekey.p12";
static YouTubeService BuildService()
{
var certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret",X509KeyStorageFlags.Exportable);
var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
{
ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
Scope = YouTubeService.Scopes.YoutubeUpload.GetStringValue() };
var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
return new YouTubeService((new BaseClientService.Initializer()
{
Authenticator = auth,
ApplicationName = "Drive API Sample",
}));
}
static void Main(string[] args)
{
var youtube = BuildService();
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = "Video title";
video.Snippet.Description = "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 = "public"; // "Video privacy (public, private, or unlisted)";
var filePath = "52224a59-2029-43fd-806b-efc434256c25.mp4";
var fileStream = new FileStream(filePath, FileMode.Open);
var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", fileStream, "video/*");
var upload = videosInsertRequest.Upload();
Console.WriteLine(upload.BytesSent);
Console.WriteLine(upload.Status);
Console.WriteLine(upload.Exception);
}
}
任何想法为什么会这样?
我正在使用Google的1.5.0.28972版.Apis DotNetOpenAuth版本:4.0.0.11165
1 回答
在几周前宣布的新版本(1.6.0-beta)中,我们提供了一个新的Auth库,它支持不同的Windows平台和不同的流程(包括服务帐户) .
在我们的OAuth2页面中阅读更多内容,特别是here . 新库不再包含DNOA中的依赖项,因此它应该非常易于使用和调试 .
希望它有所帮助,让我更新 .