gpt4 book ai didi

c# - 使用 C# 和 Google.Apis.YouTube.v3 列出 YouTube 视频

转载 作者:太空狗 更新时间:2023-10-29 22:09:17 25 4
gpt4 key购买 nike

我正在尝试使用最新版本的 Google.Apis.YouTube.v3(截至 2014 年 1 月 15 日)执行一些 YouTube 视频交互。

我在以下方面做了一个 NuGet:

  • Google.Apis.YouTube.v3
  • Google.Apis.Authentication
  • Google.Apis.Drive.v2(不是必需的,但还是得到了)

然后我尝试运行找到的代码:https://developers.google.com/youtube/v3/docs/playlistItems/list

但是,该代码具有以下引用,我似乎无法在任何最新的 NuGet 下载中找到这些引用...

  • 使用 Google.Apis.Auth.OAuth2.DotNetOpenAuth;
  • 使用 Google.Apis.Samples.Helper;

然后在代码顶部有以下注释,但这些链接没有给我任何用处。

/* 外部依赖、OAuth 2.0 支持和核心客户端库位于:*//* https://code.google.com/p/google-api-dotnet-client/wiki/APIs#YouTube_Data_API *//* 另请参阅 Google.Apis.Samples.Helper 类的 Samples.zip 文件:*//* https://code.google.com/p/google-api-dotnet-client/wiki/Downloads */

我开始相信使用 C# 玩 YouTube 的最佳方式是使用旧版本的 YouTube.v3 代码库,这些代码库与人们似乎开始工作的示例一致。

任何帮助(尤其是来自 peleyal 的帮助)将不胜感激。也许我遗漏了一些明显的东西,需要被打败......

顺便说一句,我已经下载了我的客户端 secret json 文件并成功运行了 google-api-dotnet-client-1.7.0-beta.samples.zip 文件中包含的一些示例。然而,奇怪的是,该示例 zip 文件中缺少任何 YouTube 示例。该 zip 文件中还缺少 Google.Apis.Samples.Helper 类。

有没有人有一些有用的示例代码,可以使用截至 2014 年 1 月 14 日的最新 NuGet 代码与 YouTube 进行交互?

最佳答案

所以经过大量研究、挖掘和少一点头发,我想通了一些事情。

首先,登录“Google Cloud Console”。如果您使用的是 GAE(Google App Engine)并单击您的 GAE 项目并启用“YouTube 数据 API v3”,那么您一定会一无所获!相反,退出您的 GAE 项目,并创建一个名为“API 项目”的新项目。

然后在那个 项目中,启用所需的 API,您将开始获得更好的结果。结果好多了。首先尝试 YouTube 搜索。这使您只需插入您的 API key ,而不必弄乱 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);
}

请随意用常规控制台打印 stmts 替换 CommandLine。

接下来,转到 OAuth 2.0 并尝试让您的凭据无误地通过。您需要从“凭据”部分下的“Google Cloud Console”下载您的 OAuth JSON 文件。获得此文件后,将所有名为“client_secrets.json”的文件替换为下载的 json 文件的内容。为了获得工作授权,我发现我缺少 Microsoft.Threading.Tasks.Extensions.Desktop.dll,它是允许浏览器打开一个窗口以授予 native 应用程序访问权限的 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 美分值(value)。此外,您需要在 DotNetOpenAuth 和代码中执行 NuGet,将对 Google.Apis.Auth.OAuth2.DotNetOpenAuth 的任何“使用”调用替换为“使用 DotNetOpenAuth”。

希望这对其他人有帮助。重要的是弄清楚 GAE 与新项目的关系。一旦我弄明白了这一点,正常数量的研究就开始产生结果,而不是纯粹的挫败感!!

关于c# - 使用 C# 和 Google.Apis.YouTube.v3 列出 YouTube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21132531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com