我想使用Google.Apis.YouTube.v3在YouTube上上传视频。
我的要求是在YouTube channel 上上传视频而没有最终用户登录提示。
因此,请告诉我有什么方法可以验证服务器端的用户并将视频直接上传到我在YouTube中的 channel 。
我想在MVC C#应用程序中实现
最佳答案
[HttpPost]
public async Task<ActionResult> UploadVideo(HttpPostedFileBase video, UploadVideoInfo info)
{
try
{
var httpPostedFile = Request.Files[0];
if (httpPostedFile != null)
{
UserCredential credential;
using (var stream = new FileStream("client_secret.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 = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
});
var videos = new Video();
videos.Snippet = new VideoSnippet();
videos.Snippet.Title = info.VideoTitle;
videos.Snippet.Description = info.VideoDescription;
videos.Snippet.Tags = new string[] { "tag1", "tag2" };
videos.Snippet.CategoryId = "22";
videos.Status = new VideoStatus();
videos.Status.PrivacyStatus = "public"; // or "public" or "unlisted"
var videosInsertRequest = youtubeService.Videos.Insert(videos, "snippet,status", video.InputStream, "video/*");
videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged;
videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived;
await videosInsertRequest.UploadAsync();
}
}
catch (Exception ex)
{
}
return View();
}
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);
}
关于asp.net-mvc - 使用Google.Apis.YouTube.v3在YouTube channel 上载视频而无最终用户登录提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451618/