因此,经过大量研究、挖掘和少一点头发后,我弄清楚了一些事情。
首先,登录“Google Cloud Console”。如果您使用 GAE (Google App Engine),并且点击您的 GAE 项目并启用“YouTube Data API v3”,那么您肯定一无所获!相反,退出您的 GAE 项目,然后创建一个名为“API 项目”的新项目。
然后内that项目,启用您想要的 API,您将开始获得更好的结果。更好的结果。首先尝试 YouTube 搜索。这允许您只需插入 API 密钥,而不必搞乱 OAuth2,并且它需要更少的 dll,因此它是一个很好的起点。尝试如下操作:
YouTubeService youtube = new YouTubeService(new BaseClientService.Initializer() {
ApplicationName = "{yourAppName}",
ApiKey = "{yourApiKey}",
});
SearchResource.ListRequest listRequest = youtube.Search.List("snippet");
listRequest.Q = "Loeb Pikes Peak";
listRequest.MaxResults = 5;
listRequest.Type = "video";
SearchListResponse resp = listRequest.Execute();
foreach (SearchResult result in resp.Items) {
CommandLine.WriteLine(result.Snippet.Title);
}
请随意将 CommandLine 替换为常规控制台打印 stmts。
接下来,继续使用 OAuth 2.0 并尝试让您的凭据顺利通过。您需要从“凭据”部分下的“Google Cloud Console”下载 OAuth JSON 文件。获得此文件后,将所有名为“client_secrets.json”的文件替换为下载的 json 文件的内容。为了获得工作授权,我发现我缺少 Microsoft.Threading.Tasks.Extensions.Desktop.dll,该 dll 允许浏览器打开一个窗口以授予本机应用程序访问权限,从而与您的应用程序混在一起。 YouTube 帐户。因此,如果您在授权部分遇到一些错误,请检查内部异常,这也可能是您的问题。
免责声明:下面显示的代码的下半部分来自:github.com/youtube/api-samples/blob/master/dotnet
UserCredential credential;
using (FileStream stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None,
new FileDataStore("YouTube.Auth.Store")).Result;
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
var video = new Video();
video.Snippet = new VideoSnippet();
video.Snippet.Title = "Default Video Title";
video.Snippet.Description = "Default Video Description";
video.Snippet.Tags = new string[] { "tag1", "tag2" };
video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status = new VideoStatus();
video.Status.PrivacyStatus = "unlisted"; // or "private" or "public"
var filePath = @"REPLACE_ME.mp4"; // 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;
videosInsertRequest.UploadAsync();
}
这就是我的 2 美分价值。此外,您还需要在 DotNetOpenAuth 上执行 NuGet,并在代码中将对 Google.Apis.Auth.OAuth2.DotNetOpenAuth 的任何“using”调用替换为“using DotNetOpenAuth”。
希望这对其他人有帮助。最重要的是弄清楚 GAE 与新项目的关系。一旦我弄清楚了这一点,正常数量的研究就开始产生结果,而不是纯粹的挫败感!