I would like to use the YouTube API to upload videos to a corporate account in a non-interactive program. I was trying to achieve upload without prompting for authentication and authorization (since there is no interactive user). I would appreciate if someone could validate the following assertions:
It is not possible to use Simple API Access to upload videos.
For OAuth, it is not possible to use "Use service accounts to call Google APIs on behalf of your application instead of an end-user." As described in Justin Smith's Blog post. This would have been my desired solution. Here is the code I tried to do this with. It executes, but always returns as "unauthorized" (401):
public async Task AnnounceAsync(Recording recording) { const string emailAddress = "xxxxxxx.gserviceaccount.com"; const string keyFile = @"C:\Temp\xxxxxxx-privatekey.p12"; var key = new X509Certificate2(keyFile, "notasecret", X509KeyStorageFlags.Exportable); var client = new AssertionFlowClient(GoogleAuthenticationServer.Description, key) { ServiceAccountId = emailAddress, Scope = YoutubeService.Scopes.Youtube.GetStringValue() }; var auth = new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState); var youtube = new YoutubeService(new BaseClientService.Initializer {Authenticator = auth}); var video = new Video { Snippet = new VideoSnippet { Title = recording.Title, Description = recording.Description, CategoryId = "22" }, Status = new VideoStatus { PrivacyStatus = "public", } }; using (var fileStream = new FileStream(recording.Path, FileMode.Open)) { var videosInsertRequest = youtube.Videos.Insert(video, "snippet,status", fileStream, "video/*"); videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged; videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived; await Task.Run(() => videosInsertRequest.Upload()); } }
So, I have concluded that I need to use the "Installed Application" API access methodology to move through the OAuth login process interactively at least once myself, save the refresh token and then use that refresh token in the future to get new access tokens? I know I can make this work, but it seems awkward. Is there any gotchas to be aware of if I go down this road?
Further questions:
- Any future support planned to allow Service Account OAuth use with the YouTube API?
- Could my program use the "mobile upload" feature to email videos to its YouTube account?
Thanks in advance for your input.