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();
Related
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
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.
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();
}
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.
Once the video is uploaded, how to check its processing status using C# .NET's API?
The answer should be in the Google.Apis.Youtube.v3.Data.VideoProcessingDetails, but how exactly should that object be instantiated? The following code throws a System.Net.Http.HttpRequestException:
video.ProcessingDetails = new VideoProcessingDetails();
The code above is being used in the following context:
UserCredential credential;
using (FileStream stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None,
new FileDataStore("YouTube.Auth.Store")).Result;
}
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 = Title;
video.Snippet.Description = Description;
video.Snippet.Tags = Tags;
video.ProcessingDetails = new VideoProcessingDetails(); // Here it throws an exception
video.Snippet.CategoryId = CategotyId;
video.Status = new VideoStatus();
video.Status.PrivacyStatus = PrivacyStatus;
var filePath = FilePath;
The idea, is once the video is uploaded, check in a loop if the video is still processing and perform other operations on it once the processing is done.
UPDATE: I figured out that if the object is instantiated after the video is uploaded (i.e. in the private static void videosInsertRequest_ResponseReceived() method, for example), the exception is gone, but once I try accessing video.ProcessingDetails.ProcessingProgress.TimeLeftMs.Value, it says that that value is null.
Thanks.
In my own work, I have found that when ProcessingDetails.ProcessingStatus equals "terminated", all of the other properties in ProcessingDetails are equal to nothing (i.e. null). I believe that this means that YouTube has finished processing the video.
I usually check a Google API v3 property to see if it is null before attempting to use it.