Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private async Task Run()
- {
- UserCredential credential;
- using (var stream = new FileStream(Server.MapPath("client_secrets.json"), FileMode.Open, FileAccess.Read))
- {
- var secrets = GoogleClientSecrets.Load(stream).Secrets;
- GoogleAuthorizationCodeFlow.Initializer initializer = new GoogleAuthorizationCodeFlow.Initializer
- {
- ClientSecrets = secrets
- };
- var scopes = new[] { Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeUpload };
- Google.Apis.Util.Store.IDataStore dataStore = null;
- var user = "user";
- initializer.Scopes = scopes;
- initializer.DataStore = (dataStore ?? new Google.Apis.Util.Store.FileDataStore(Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.Folder, false));
- GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(initializer);
- var codeReceiver = new WebCodeReceiver();
- codeReceiver.RedirectUri = "http://localhost:2627/oauth2callback/";
- credential = await new Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp(flow, codeReceiver).AuthorizeAsync(user, CancellationToken.None).ConfigureAwait(false);
- }
- var youtubeService = new YouTubeService(new BaseClientService.Initializer()
- {
- HttpClientInitializer = credential,
- ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
- });
- var video = new Video();
- video.Snippet = new VideoSnippet();
- video.Snippet.Title = titleBox.Text;
- video.Snippet.Description = DescriptionBox.Text;
- 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"
- //if (FileUpload1.HasFile)
- //{
- var filePath = Server.MapPath("~/Uploads/22.mp4");
- FileUpload1.SaveAs(filePath);
- 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();
- }
- //}
- }
- void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
- {
- switch (progress.Status)
- {
- case UploadStatus.Uploading:
- Response.Write(string.Format("{0} bytes sent.", progress.BytesSent));
- break;
- case UploadStatus.Failed:
- Response.Write(string.Format("An error prevented the upload from completing.\n{0}", progress.Exception));
- break;
- }
- }
- void videosInsertRequest_ResponseReceived(Video video)
- {
- Response.Write(string.Format("Video id '{0}' was successfully uploaded.", video.Id));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement