Youtube api upload video.net
本文关键字:net video upload api Youtube | 更新日期: 2023-09-27 18:16:40
我试图创建一个网站,你可以从你的设备上传到youtube的视频,但能够查看你从我的网站上传的那些特定的视频。(也有更多的目的)
我使用。net c#,我有我的oauth密钥和开发人员api密钥,下载的json文件等。我下载了谷歌的样本代码,但似乎不能让它工作,不完全确定它是如何工作的。
谁能帮我解决这个问题/给我解释一下吗?using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
/// <summary>
/// Summary description for Video_Upload
/// </summary>
public class Video_Upload
{
public Video_Upload()
{
//
// TODO: Add constructor logic here
new Video_Upload().Run().Wait();
}
private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("Client_id_googleApi.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
});
//VIDEO INFO AND DETAILS
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = "Test Video 1";
video.Snippet.Description = "Testing Video Upload";
video.Snippet.Tags = new string[] { "Test", "First" };
video.Snippet.CategoryId = "17";//category id for sport // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Snippet.ChannelId = "UCfvR-wqeoHmAGrHnoQRfs9w";
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "public"; // or "private" or "public"
var filePath = @"C:'Users'siobhan'Documents'Visual Studio 2015'WebSites'FYP_November'GP010149.avi"; // Replace with path to actual movie file.
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();
}
void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
switch (progress.Status)
{
case UploadStatus.Uploading:
Console.WriteLine(progress.BytesSent, " bytes sent.");
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);
}
}
}
它说'void不能这样使用'
因为你试图在另一个方法中声明一个方法:
private async Task Run()
{
//...
void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
//...
}
//...
}
在c#中是无效的。方法是在类内部声明的,并且基本上是并排的。如:
private async Task Run()
{
//...
}
void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress)
{
//...
}