sanjosep43
sanjosep43

Reputation: 617

Google .NET Client Youtube v3 error

I get this following error when trying to upload a video to my youtube channel:

System.ArgumentException: Precondition failed.: !string.IsNullOrEmpty(authorization.RefreshToken) at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) at Microsoft.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at Google.Apis.Upload.ResumableUpload1.d__0.MoveNext() in c:\code.google.com\google-api-dotnet-client\default_3\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis\Apis[Media]\Upload\ResumableUpload.cs:line 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);
}
}

Any ideas why this is happening?
I'm using version 1.5.0.28972 of Google.Apis DotNetOpenAuth version: 4.0.0.11165

Upvotes: 0

Views: 395

Answers (1)

peleyal
peleyal

Reputation: 3512

In the new release (1.6.0-beta) which was announced several weeks ago, (Read more in our announcements blog here) we presented a new Auth library which supports different Windows platforms and different flows (including service account).

Read more in our OAuth2 page and specifically here. The new library doesn't include a dependency in DNOA any more, so it should be very easy to use and debug.

Hope it is helpful, keep me updated.

Upvotes: 1

Related Questions