I want to upload videos to my YouTube channel programmatically, but when I try to upload using API Key, I got the error in response, that this API don't support API Key.
When I try using OAuth, my program always open browser and open Google page with grants confirmation, but I want to upload videos without any confirmations.
Also I tried use Service Account, but in the documentation wrote that API don't support service accounts.
How I can upload videos without any confirmations (for example, using credentials)?
EDITED
I setting API Key using the next code:
var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*");
videosInsertRequest.Key = "MY_KEY";
And also I tried using OAuth2 login using the next code:
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
});
API keys only give you access to public data, you could use an api key to search for YouTube public videos. However uploading a video requires consent of the owner of the account.
To get consent we use OAUTH2 to request consent of the user, you will get an access token and a refresh token back. The access token will expire after an hour however you can use the refresh token to request a new access token when ever you need. you just need to store it and use it to request a new access token.
In your case using C# for a desktop applicaitn your code should look something like this
The "user" denotes the user who has logged in, their credentials are stored by default in %appData% so the next time this application runs it will not request authorization. So as long as you authorize it once your code will continue to work.
The YouTube api does not support service accounts.
There is no way to get around popping up the consent browser to the user to request consent for the initial request as long as you store the refresh token and configure your code correctly you should not see it again.