Advertisement
Guest User

Untitled

a guest
Oct 17th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. private async Task Run()
  2. {
  3. UserCredential credential;
  4. using (var stream = new FileStream(Server.MapPath("client_secrets.json"), FileMode.Open, FileAccess.Read))
  5. {
  6. var secrets = GoogleClientSecrets.Load(stream).Secrets;
  7.  
  8. GoogleAuthorizationCodeFlow.Initializer initializer = new GoogleAuthorizationCodeFlow.Initializer
  9. {
  10. ClientSecrets = secrets
  11. };
  12.  
  13. var scopes = new[] { Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeUpload };
  14. Google.Apis.Util.Store.IDataStore dataStore = null;
  15. var user = "user";
  16.  
  17. initializer.Scopes = scopes;
  18. initializer.DataStore = (dataStore ?? new Google.Apis.Util.Store.FileDataStore(Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.Folder, false));
  19. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(initializer);
  20.  
  21. var codeReceiver = new WebCodeReceiver();
  22. codeReceiver.RedirectUri = "http://localhost:2627/oauth2callback/";
  23. credential = await new Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp(flow, codeReceiver).AuthorizeAsync(user, CancellationToken.None).ConfigureAwait(false);
  24.  
  25.  
  26. }
  27.  
  28. var youtubeService = new YouTubeService(new BaseClientService.Initializer()
  29. {
  30. HttpClientInitializer = credential,
  31. ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
  32. });
  33.  
  34. var video = new Video();
  35. video.Snippet = new VideoSnippet();
  36. video.Snippet.Title = titleBox.Text;
  37. video.Snippet.Description = DescriptionBox.Text;
  38. video.Snippet.Tags = new string[] { "tag1", "tag2" };
  39. video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
  40. video.Status = new VideoStatus();
  41. video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
  42. //if (FileUpload1.HasFile)
  43. //{
  44. var filePath = Server.MapPath("~/Uploads/22.mp4");
  45. FileUpload1.SaveAs(filePath);
  46.  
  47. using (var fileStream = new FileStream(filePath, FileMode.Open))
  48. {
  49. var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
  50. videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
  51. videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
  52.  
  53. await videosInsertRequest.UploadAsync();
  54. }
  55. //}
  56. }
  57.  
  58. void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
  59. {
  60. switch (progress.Status)
  61. {
  62. case UploadStatus.Uploading:
  63. Response.Write(string.Format("{0} bytes sent.", progress.BytesSent));
  64. break;
  65.  
  66. case UploadStatus.Failed:
  67. Response.Write(string.Format("An error prevented the upload from completing.\n{0}", progress.Exception));
  68. break;
  69. }
  70. }
  71.  
  72. void videosInsertRequest_ResponseReceived(Video video)
  73. {
  74. Response.Write(string.Format("Video id '{0}' was successfully uploaded.", video.Id));
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement