I'm using the Youtube .Net libray I downloaded from Nuget.
I created a windows service which is checking a folder to upload new videos to youtube. I did the tests using a console application and for this case the user had to do authorization manually on a web browser, after that I could upload videos to youtube without any problem. The issue is when I'm trying to use the offline access.
As I'm running my code on a windows service I cannot get a the manual authorization from the user so I created my access token following this answer: https://stackoverflow.com/a/20689423/2277059
The thing is that I didn't find any example of code using the standalone version so I'm a bit lost. With my current code I'm getting all the time Error:"unauthorized_client" when trying to upload the video. The JSON file you can see on my code "client_secrets.json" is the one you automatically generate when creating the credentials for the YouTube API V3.
I tried this as well: https://stackoverflow.com/a/43895931/2277059, but got same error.
Right now I'm not refreshing the token from code, just doin it on the OAuth Playground and testing the service. Once it worked I'll research about how refresh the token with every query.
When creating the credentials I selected the type "Other" as this is a windows service.
Is it something wrong with my code or am I missing something on the configuration?
This is my code:
var token = new TokenResponse()
{
AccessToken = "werwdsgfdg...",
ExpiresInSeconds = 3600,
RefreshToken = "3/dsdfsf...",
TokenType = "Bearer"
};
Log.Info("Generating user credentials and secrets");
UserCredential credential;
string credentialsPath = System.AppDomain.CurrentDomain.BaseDirectory + "client_secrets.json";
using (var stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read))
{
credential = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = GoogleClientSecrets.Load(stream).Secrets,
Scopes = new[] { YouTubeService.Scope.YoutubeUpload }
}
), EsSettings.YoutubeUser, token);
}
Log.Info("Generating youtube service");
//GoogleAuthorizationCodeFlow
YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
Log.Info("Uploading...");
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();
}