Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am working on an Web application that uploads a video to the users Youtube Channel. I used the code which is given in the google Youtube API guide.

C#
static void Main(string[] args)
{
  Console.WriteLine("YouTube Data API: Upload Video");
  Console.WriteLine("==============================");

  try
  {
    new UploadVideo().Run().Wait();
  }
  catch (AggregateException ex)
  {
    foreach (var e in ex.InnerExceptions)
    {
      Console.WriteLine("Error: " + e.Message);
    }
  }

  Console.WriteLine("Press any key to continue...");
  Console.ReadKey();
}

private async Task Run()
{
  UserCredential credential;
  using (var stream = new FileStream("client_secrets.json", 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
    );
  }

  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 = "Default Video Title";
  video.Snippet.Description = "Default 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 = "unlisted"; // or "private" or "public"
  var filePath = @"REPLACE_ME.mp4"; // Replace with path to actual movie file.

  using (var fileStream = new FileStream(filePath, FileMode.Open))
  {
    var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");


    await videosInsertRequest.UploadAsync();
  }
}


What I have tried:

This code works fine when I run it in visual studio locally and the video gets uploaded to my Youtube channel. But the problem which I face is when I host the application in the IIS at a server machine it throws a Access Denied exception to the folder

'C:\Windows\system32\config\systemprofile'

I gave the permission to the folder and now it threw an different exception

"Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&response_type=code&client_id=MY_CLIENT_ID&redirect_uri=http%3A%2F%2Flocalhost%3A62196%2Fauthorize%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.upload" for authorization. See inner exception for details."
Posted
Updated 21-Mar-18 22:45pm
Comments
Patrice T 8-Mar-18 0:32am    
We are not Google/YouTube support.
Rokesh Kumar 8-Mar-18 6:05am    
I just wanted to know if someone else has come across this issue
F-ES Sitecore 8-Mar-18 4:29am    
This;

FileStream("client_secrets.json"...

probably isn't doing what you think it's doing if this code is hosted in IIS. What folder is it going to look in for your json file?
Rokesh Kumar 8-Mar-18 6:04am    
The Json file is inside the application folder and it has permission to access it, the problem is it wants the browser to open up with a redirect URI but it does not.
F-ES Sitecore 8-Mar-18 6:15am    
"Application folder"? What is the "application folder" for a site running under IIS? Remember that your code in IIS is running inside a host application (IIS) and when a file is asked for it is the host doing the requesting, not your site.

Also if this code is literally opening a browser then that's not going to work inside a website either as your code (again) is running inside a host service and that service has no access to the desktop.

Just because your code runs in a console doesn't mean you can put that code in a website and it will still run.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900