public async Task <IPlaylist> AddNewPlaylist(string title, string description, PrivacyStatus privacyStatus) { // Create a new, private playlist in the authorized user's channel. if (string.IsNullOrEmpty(title)) { return(null); } try { var newPlaylist = new Playlist(); newPlaylist.Snippet = new PlaylistSnippet(); newPlaylist.Snippet.Title = title; newPlaylist.Snippet.Description = description; newPlaylist.Status = new PlaylistStatus(); newPlaylist.Status.PrivacyStatus = privacyStatus.ToString().ToLower();// "public"; newPlaylist = await _youTubeService.Playlists.Insert(newPlaylist, "snippet,status,contentDetails,id").ExecuteAsync(); return(new MPlaylist(newPlaylist)); } catch (Exception e) { throw new LiteTubeException(e); } }
private async System.Threading.Tasks.Task <bool> UploadVideo(string title, string desc, string[] tags, string categoryId, PrivacyStatus ps, string filePath) { try { InfoFormat("Uploading the video file {0} to YouTube started...", filePath); Info("Authentication started..."); UserCredential credential; using (var stream = new FileStream(ClientSecrets, 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 ); } Info("Authentication succeeded."); var youtubeService = new YouTubeService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); var video = new Video { Snippet = new VideoSnippet() }; video.Snippet.Title = title; video.Snippet.Description = desc; video.Snippet.Tags = tags; video.Snippet.CategoryId = categoryId; // See https://developers.google.com/youtube/v3/docs/videoCategories/list video.Status = new VideoStatus { PrivacyStatus = ps.ToString().ToLower() // "unlisted" or "private" or "public" }; using (var fileStream = new FileStream(filePath, FileMode.Open)) { var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*"); videosInsertRequest.ResponseReceived += VideosInsertRequest_ResponseReceived; var res = videosInsertRequest.Upload(); if (res.Exception != null) { ErrorFormat("An error occured while uploading the file {0}: {1}", filePath, res.Exception.Message); return(false); } } InfoFormat("Uploading the video file {0} to YouTube succeeded.", filePath); return(true); } catch (ThreadAbortException) { throw; } catch (Exception e) { ErrorFormat("An error occured while uploading the video file {0}: {1}", filePath, e.Message); return(false); } }
private async Task Run(string title, string description, string[] tags, PrivacyStatus privacyStatus, IFormFile file) { UserCredential userCredential; GoogleAuthorizationCodeFlow.Initializer initializer = new GoogleAuthorizationCodeFlow.Initializer(); initializer.ClientSecrets = new ClientSecrets() { ClientId = _youtubeClientSecret.client_id, ClientSecret = _youtubeClientSecret.client_secret }; initializer.Scopes = new string[1] { YouTubeService.Scope.YoutubeUpload }; //var token = new TokenResponse { RefreshToken = _youtubeClientSecret.token_uri }; //userCredential = new UserCredential(new GoogleAuthorizationCodeFlow( // new GoogleAuthorizationCodeFlow.Initializer // { // ClientSecrets = initializer.ClientSecrets // }), // "user", // token); string token = await GenerateTokenAsync(); if (token != "") { userCredential = new UserCredential(new GoogleAuthorizationCodeFlow(initializer), "me", new TokenResponse() { RefreshToken = token, TokenType = "Bearer", ExpiresInSeconds = new long?(3599L), AccessToken = token }); YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer() { HttpClientInitializer = userCredential, ApplicationName = Assembly.GetExecutingAssembly().GetName().Name }); Video video = new Video() { Snippet = new VideoSnippet() }; video.Snippet.Title = title; video.Snippet.Tags = tags; video.Snippet.Description = description; video.Snippet.CategoryId = ""; video.Status = new VideoStatus { PrivacyStatus = privacyStatus.ToString() }; await Task.Run(() => { VideosResource.InsertMediaUpload insertMediaUpload = youtubeService.Videos.Insert(video, "snippet,status", file.OpenReadStream(), "video/*"); insertMediaUpload.ProgressChanged += videosInsertRequest_ProgressChanged; insertMediaUpload.ResponseReceived += VideosInsertRequest_ResponseReceived; insertMediaUpload.UploadAsync().Wait(); }); } else { return; } }
private async Task <string> RunPostAsync(string title, string description, string[] tags, PrivacyStatus privacyStatus, IFormFile file) { try { var retryCount = 0; GoogleAuthorizationCodeFlow.Initializer initializer = new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets() { ClientId = _youtubeClientSecret.client_id, ClientSecret = _youtubeClientSecret.client_secret }, Scopes = new string[1] { YouTubeService.Scope.YoutubeUpload } }; ReGenerateToken: if (retryCount > 3) { return(""); } var token = await GenerateTokenAsync(); if (token == "") { retryCount += 1; goto ReGenerateToken; } var userCredential = new UserCredential(new GoogleAuthorizationCodeFlow(initializer), "me", new TokenResponse() { RefreshToken = token, TokenType = "Bearer", ExpiresInSeconds = new long?(3599L), AccessToken = token }); YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer() { HttpClientInitializer = userCredential, ApplicationName = Assembly.GetExecutingAssembly().GetName().Name }); Video video = new Video() { Snippet = new VideoSnippet() }; video.Snippet.Title = title; video.Snippet.Tags = tags; video.Snippet.Description = description; video.Snippet.CategoryId = ""; video.Status = new VideoStatus { PrivacyStatus = privacyStatus.ToString() }; VideosResource.InsertMediaUpload insertMediaUpload = youtubeService.Videos.Insert(video, "snippet,status", file.OpenReadStream(), "video/*"); // insertMediaUpload.ProgressChanged += PostVideosInsertRequest_ProgressChanged; // insertMediaUpload.ResponseReceived += PostVideosInsertRequest_ResponseReceived; await insertMediaUpload.UploadAsync(); return(insertMediaUpload.ResponseBody?.Id); } catch (Exception e) { return(""); } }