Upload video to specific channel / Youtube

3.8k views Asked by At

I'm trying to upload videos to Youtube. Successfully uploaded videos to account's channel. After that I created new channel with name "Deneme1". And I tried to upload to that channel with api; but uploaded to main.

My code:

    public static string UploadVideo(string FilePath, string Title, string Description)
    {
        YouTubeRequestSettings settings;
        YouTubeRequest request;
        string devkey = "api key";            
        string username = "[email protected]";
        string password = "password";
        settings = new YouTubeRequestSettings("Deneme1", devkey, username, password) { Timeout = 10000000 };
        request = new YouTubeRequest(settings);

        Video newVideo = new Video();
        newVideo.Title = Title;
        newVideo.Description = Description;
        newVideo.Private = true;
        newVideo.YouTubeEntry.Private = false;
        newVideo.Keywords = "asd";
        newVideo.Tags.Add(new MediaCategory("Sports", YouTubeNameTable.CategorySchema));
        newVideo.YouTubeEntry.MediaSource = new MediaFileSource(FilePath, "video/flv");
        Video createdVideo = request.Upload(newVideo);
        return createdVideo.VideoId;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
         try
        {
            string videopath, videotitle, videodesc;
            videopath = @"C:\Users\Ercin\Dropbox\Cloudy\Visual Studio\Projects\videoupload\videoupload\badstart.flv";
            videotitle = "test title";
            videodesc = "test description";
            UploadVideo(videopath, videotitle, videodesc);
        }
        catch (Exception exception)
        {
            Response.Write("Upload failed: " + exception.Message);
        }

Any help will be fantastic!

2

There are 2 answers

1
thiago.adriano26 On

This code bellow work's fine for me, to upload to specific channel I put of ChannelId in username.

public static Google.Apis.YouTube.v3.YouTubeService AuthenticateOaut(string clientId, string clientSecret, string userName)
        {

            string[] scopes = new string[] { Google.Apis.YouTube.v3.YouTubeService.Scope.Youtube,  // view and manage your YouTube account
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeForceSsl,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.Youtubepartner,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubepartnerChannelAudit,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeReadonly,
                                             Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeUpload};

            try
            {
                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , userName
                                                                                             , CancellationToken.None
                                                                                             , new FileDataStore("Daimto.YouTube.Auth.Store")).Result;

                Google.Apis.YouTube.v3.YouTubeService service = new Google.Apis.YouTube.v3.YouTubeService(new Google.Apis.YouTube.v3.YouTubeService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "Web client 1",

                });
                return service;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                return null;

            }

        }
0
NoOne On

Starting from the beginning (which will be helpful for some folks) I will tell you what I have done:

  • You get your authorization credentials (I have used OAuth2.0): https://developers.google.com/youtube/registering_an_application#Create_OAuth2_Tokens
  • You use the YouTube v3 API (there is a nuget package).
  • You write your code which is pretty much what thiago.adriano26 has in his answer.
  • When running your app for the first time it will open the browser for your to verify and select the channel you want (I am not sure why Google did that, since already you specify the user ID in your code, but that's what they did anyway...).
  • Once you complete this step, a token is generated at : C:\Users\User\AppData\Roaming\Google.Apis.Auth\Google.Apis.A‌​uth.OAuth2.Responses‌​.TokenResponse-<UserId>. This seems to link the used UserId to the actual channel. As long as this is there, this link between the two will exist. Deleting it, or using another UserId (each channel has its own UserId and ChannelId see here: https://support.google.com/youtube/answer/3250431?hl=en) will allow you to select a new channel from the browser.