1
votes

I'm using the YouTube API V3 in an MVC web application. The aim is for my web users to upload a video to MY YouTube channel using MY OAuth credentials. After the user uploads their video to my web server, I need the upload between my web server and YouTube to be automated with no user intervention from myself.

My (first draft) code is below. My questions are:

  1. UPDATE - Resolved below. Whenever I try to delete a video I get an 'Unauthorised' error. When I check the scope of the token it only has 'YoutubeUpload' and not 'Youtube'. Is there any way to resolve this?

  2. I created an OAuth client credential of type 'Other' and exported the JSON (I specified 'Other' instead of 'Web Application' - is this correct?): https://console.developers.google.com/apis/credentials

When I try and upload a video, I get redirected to 'Authorise the App' saying "My Company wants access to your YouTube Channel." The trouble is, i DONT want to access anyone's channel. I just want them to be able to upload to MY channel. Is this possible, and if so how?

    public async Task<string> UploadVideoToYouTube(Stream filestream)
    {
        UserCredential credential;

        string cs = HttpContext.Current.Server.MapPath("~/client_secrets.json");

        using (var stream = new FileStream(cs, 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.Youtube,
                    YouTubeService.Scope.YoutubeUpload },
                "user",
                CancellationToken.None
            );
        }

       //check scope with this URL
        //https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={ACCESS-TOKEN}
        // This bit checks if the token is out of date, 
        // and refreshes the access token using the refresh token.
        if (credential.Token.IsExpired(SystemClock.Default))
        {
            if (!await credential.RefreshTokenAsync(CancellationToken.None))
            {
                Console.WriteLine("No valid refresh token.");
            }
        }

        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" };
        //https://gist.github.com/dgp/1b24bf2961521bd75d6c
        //26 - How-to & Style
        video.Snippet.CategoryId = "26"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
        video.Status = new VideoStatus();
        video.Status.PrivacyStatus = "private"; // "unlisted" or "private" or "public"

        var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", filestream, "video/*");
        videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
        videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;

        await videosInsertRequest.UploadAsync();

        return videosInsertRequest.ResponseBody.Id;
    }

    void videosInsertRequest_ResponseReceived(Video video)
    {
        //http://www.youtube.com/watch?v={VIDEO-ID}
        Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);

    }

    public async Task DeleteVideoFromYouTube(string id)
    {
        UserCredential credential;

        string cs = HttpContext.Current.Server.MapPath("~/client_secrets.json");

        using (var stream = new FileStream(cs, 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.Youtube,                    
                YouTubeService.Scope.YoutubeUpload  },
                "user",
                CancellationToken.None
            );
        }

        // This bit checks if the token is out of date, 
        // and refreshes the access token using the refresh token.
        if (credential.Token.IsExpired(SystemClock.Default))
        {
            if (!await credential.RefreshTokenAsync(CancellationToken.None))
            {
                Console.WriteLine("No valid refresh token.");
            }
        }

        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });

        var videosDeleteRequest = youtubeService.Videos.Delete(id);
        await videosDeleteRequest.ExecuteAsync();


    }

    void videosInsertRequest_ProgressChanged(IUploadProgress progress)
    {
        switch (progress.Status)
        {
            case UploadStatus.Uploading:
                Console.WriteLine("{0} bytes sent.", progress.BytesSent);
                break;

            case UploadStatus.Failed:
                Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
                break;
        }
    }

UPDATE Resolution to the first question was to go here: https://myaccount.google.com/permissions and delete the authorised YouTube app. It must have been authorised previously without the YouTubeService.Scope.Youtube scope.

However, when I deleted the account and ran my web application again, it prompted me to authenticate again using OAuth. I don't want it to do this. I want it to automatically authenticate as me (for all users). Which is my second question that remains unanswered....

2
Did you get the answer?sachin
How to Upload to My Youtube Channel?Hackbal Teamz

2 Answers

0
votes

the key is in this part of code:

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
    );

you have to specify correct user name, not just "user"

-1
votes

This is following Sample code for Uploading video on YouTube

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

namespace Google.Apis.YouTube.Samples
{
  /// <summary>
  /// YouTube Data API v3 sample: upload a video.
  /// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
  /// See https://developers.google.com/api-client-library/dotnet/get_started
  /// </summary>
  internal class UploadVideo
  {
    [STAThread]
    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/*");
        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:
          Console.WriteLine("{0} bytes sent.", progress.BytesSent);
          break;

        case UploadStatus.Failed:
          Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception);
          break;
      }
    }

    void videosInsertRequest_ResponseReceived(Video video)
    {
      Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id);
    }
  }
}