How to upload youtube videos with side channels using youtube api - C # - c#

I'm using YouTube's API to upload videos. However, my YouTube account has two channels, and I'm only able to successfully upload to the main channel, not the second channel.
Can anyone tell me what I need to do to upload to the second channel?
The Code:
private async Task Run(String title,String mota,String[] tag,String filepath)
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.YoutubeUpload},
"user",
CancellationToken.None
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
var video = new Google.Apis.YouTube.v3.Data.Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = title;
video.Snippet.Description = mota;
video.Snippet.Tags = tag;
video.Snippet.CategoryId = "22";
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "unlisted";
var filePath = 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();
}
}

You must authorize the correct account in the OAuth flow. If the second account circled in red is a CMS account you must use the onBehalfOfContentOwner parameter in your API call. Some calls also have the forUsername parameter which allow you to call on behalf of users, by supplying their username.
For example the channels.list mehtod has both a onBehalfOfContentOwner parameter andforUsername parameter.

Related

How to upload video to YouTube using C#(WPF) by Google account login, so that every one can use my App

I tired a simple with client_secret.json, and it can work fine.
but question is I don't need to login with google account anymore,
how can I do if I want to upload a video to YouTube using other people's account?
private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
//new GoogleAuthorizationCodeFlow.Initializer(),
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,
});/*youtubeService.HttpClient.Timeout = TimeSpan.FromMinutes(15);*/
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = "look the video111.";
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 = "public"; // or "private" or "public"
var filePath = #"C:\Users\plu\Videos\Captures\simple.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();
}
}
"user",
User denoted the name of the user who you are logging in as. Filedatastore will store the user in %appdata% on your machine. If you want to login as a different user just change "user" to a different string denoting a new user. To switch back to the original user just change it back to "user"
you can read more about file data store in my article here

youtube-v3-api "Failed to launch browser with "https://accounts.google.com/o/oauth2/v2/auth?

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.
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();
}
}
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."

YouTube Data API for Uploading Videos not working in mvc

i am trying to upload a video to YouTube by using YouTube data API, from my application but unfortunately the API which provided by YouTube developers
https://developers.google.com/youtube/v3/code_samples/dotnet#upload_a_video
is not working before three weeks i tested the code and it was working fine but now when i try to finally use it in my application its causing a problem all the code is working fine but when the video uploading process starts it's became loading and loading and the video doesn't uploaded i google it but not find any help. if some one can help me ? here is my code which i am using
private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("G:\\client_secret_783534382593-0cn4gm229raqq97kdu0ghsj9hqfsc5o1.apps.googleusercontent.com.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 = "ttry";
video.Snippet.Description = " Video Description";
video.Snippet.Tags = new string[] { "tag1s", "tag2s" };
video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "public"; // or "private" or "publi
var filePath = "F:\\Dna.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);
}
}
}
and i am simply calling it from this action
public ActionResult Contact()
{
Run().Wait();
return View();
}
i wonder that why it is not working ? when i debug it then all the things and parameters are fine even credentials are also fine but at the end the file is not uploading even i am selecting 1 MB file and i also included all the necessary NuGet packages kindly some one help me please ?
the problem is solve by simply adding some bit of code in Get Function
public async Task<ActionResult> Contact()
{
await Run();
return View();
}

Trouble deleting video from YouTube API in c#

I'll apologize in advance for likely missing something obvious, but I've been struggling to find adequate documentation on the .NET c# implementation of the v3 youtube API. I'm trying to delete a video off the ID that I get back from uploading it after waiting a minute. This is purely for testing purposes as the final software needs to be significantly more complicated, but I've reduced this problem down to the following code. The upload runs perfectly, but the delete call at the end throws a 403. The id string is correct that it's using as I've tried the interface on the API test page. Any thoughts or ideas would be helpful. thanks a bunch in advance
UserCredential credential;
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "id",
ClientSecret = "secret"
},
new[] { YouTubeService.Scope.YoutubeForceSsl },
"email",
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 = "test video";
video.Snippet.Description = "Default Video Description";
video.Snippet.Tags = new string[] { "tag1", "tag2" };
video.Snippet.CategoryId = "28";
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "unlisted";
var filePath = "path";
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();
}
// uploadID is set globally by response and is correct
Console.WriteLine(uploadID);
Thread.Sleep(60000);
youtubeService.Videos.Delete(uploadID).Execute();

How to upload video to youtube specific channelID in C#

I am able to successfully upload a video to my youtube account in C# using the following code:
private async Task Run(string videoFile, string dateStamp)
{
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.Youtubepartner},
"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 = string.Format("{0} - La Jolla boat launch timelapse", dateStamp);
video.Snippet.Description = "Daily TimeLapse of the boat launch area.";
video.Snippet.Tags = new string[] { "LaJolla", "timelapse" };
video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Snippet.ChannelId = "UC8LB7gACPEoNxdAK6LM-L6A";
video.Snippet.ChannelTitle = "TimeLapse La Jolla";
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "public"; // or "private" or "public"
var filePath = videoFile; // 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();
}
}
However, I have 3 channels in my youtube account and would like to upload to a specific channel. The code above will upload to my main account without issue, but not to the channel ID supplied. The channel name and ID are correct - but this is not working as I expected the video to appear as a new upload in the channel. I am not able to find additional information for this functionality. Any suggestions?
Code is from youtube API samples.
Thanks for looking.

Categories