使用 c# Asp .net MVC 将视频上传到 Youtube
Upload Video to Youtube with c# Asp .net MVC
我在测试上传视频到 YouTube 后遇到了这个错误
{"The remote server returned an error: (401) Unauthorized."}
我不知道我的代码有什么问题...
YouTubeRequestSettings settings = new YouTubeRequestSettings("VideoToYoutube", "AIzaSyBiXxL5nS6IjYRGJUhDdaYdWGqAGwOvD8A");
YouTubeRequest request = new YouTubeRequest(settings);
Video newVideo = new Video();
newVideo.Title = "Teste";
newVideo.Tags.Add(new MediaCategory("teste", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "Teste";
newVideo.Description = "Teste";
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory("teste, teste",
YouTubeNameTable.DeveloperTagSchema));
newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
newVideo.YouTubeEntry.MediaSource = new MediaFileSource("C:\Users\tadriano\Documents\streaming\mov_bbb.mp4", "video/mp4");
var createdVideo = request.Upload(newVideo);
return View();
}
我已在我的配置中添加 *localhost*、127.0.0.1 和 * 作为可接受的 HTTP 引荐来源网址。
有人可以帮助我吗?
我用下面的代码解决了这个问题,并创建了一个 Api 密钥 OAuth 2,应用程序类型为 outher。
public static Google.Apis.YouTube.v3.YouTubeService AuthenticateOaut(string clientId, string clientSecret, string userName)
{
string[] scopes = new string[] { Google.Apis.YouTube.v3.YouTubeService.Scope.Youtube, // view and manage your YouTube account
Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeForceSsl,
Google.Apis.YouTube.v3.YouTubeService.Scope.Youtubepartner,
Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubepartnerChannelAudit,
Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeReadonly,
Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeUpload};
try
{
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore("Daimto.YouTube.Auth.Store")).Result;
Google.Apis.YouTube.v3.YouTubeService service = new Google.Apis.YouTube.v3.YouTubeService(new Google.Apis.YouTube.v3.YouTubeService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Web client 1",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
return null;
}
}
我在测试上传视频到 YouTube 后遇到了这个错误
{"The remote server returned an error: (401) Unauthorized."}
我不知道我的代码有什么问题...
YouTubeRequestSettings settings = new YouTubeRequestSettings("VideoToYoutube", "AIzaSyBiXxL5nS6IjYRGJUhDdaYdWGqAGwOvD8A");
YouTubeRequest request = new YouTubeRequest(settings);
Video newVideo = new Video();
newVideo.Title = "Teste";
newVideo.Tags.Add(new MediaCategory("teste", YouTubeNameTable.CategorySchema));
newVideo.Keywords = "Teste";
newVideo.Description = "Teste";
newVideo.YouTubeEntry.Private = false;
newVideo.Tags.Add(new MediaCategory("teste, teste",
YouTubeNameTable.DeveloperTagSchema));
newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
newVideo.YouTubeEntry.MediaSource = new MediaFileSource("C:\Users\tadriano\Documents\streaming\mov_bbb.mp4", "video/mp4");
var createdVideo = request.Upload(newVideo);
return View();
}
我已在我的配置中添加 *localhost*、127.0.0.1 和 * 作为可接受的 HTTP 引荐来源网址。
有人可以帮助我吗?
我用下面的代码解决了这个问题,并创建了一个 Api 密钥 OAuth 2,应用程序类型为 outher。
public static Google.Apis.YouTube.v3.YouTubeService AuthenticateOaut(string clientId, string clientSecret, string userName)
{
string[] scopes = new string[] { Google.Apis.YouTube.v3.YouTubeService.Scope.Youtube, // view and manage your YouTube account
Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeForceSsl,
Google.Apis.YouTube.v3.YouTubeService.Scope.Youtubepartner,
Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubepartnerChannelAudit,
Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeReadonly,
Google.Apis.YouTube.v3.YouTubeService.Scope.YoutubeUpload};
try
{
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore("Daimto.YouTube.Auth.Store")).Result;
Google.Apis.YouTube.v3.YouTubeService service = new Google.Apis.YouTube.v3.YouTubeService(new Google.Apis.YouTube.v3.YouTubeService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Web client 1",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
return null;
}
}