使用 C# 和 Google.Apis.YouTube.v3 列出 YouTube 视频 [英] List YouTube videos using C# and Google.Apis.YouTube.v3

查看:24
本文介绍了使用 C# 和 Google.Apis.YouTube.v3 列出 YouTube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to perform some YouTube video interaction using the latest version of Google.Apis.YouTube.v3 (as of Jan 15, 2014).

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

I have done a NuGet on the following:

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

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

I then attempted to run the code found on: https://developers.google.com/youtube/v3/docs/playlistItems/list

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

However, the code has the following references which I can't seem to find in any of the latest NuGet downloads...

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

然后在代码顶部有以下注释,但链接使我没有任何用处.

Then there's the following comment at the top of the code, but the links lead me to nothing useful.

/* 外部依赖项、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 代码库,这些代码库与人们似乎开始使用的示例一致.

I'm beginning to believe the best way to play with YouTube using C# is to use older versions of the YouTube.v3 codebase that coincide with examples folks have seemed to get working.

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

Any help (esp from peleyal) would be much appreciated. Perhaps I'm missing something obvious and need to be beat over the head...

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

BTW, I have downloaded my client secret json file and successfully run a few of the examples contained within the google-api-dotnet-client-1.7.0-beta.samples.zip file. However, strangely missing from that samples zip file are any YouTube samples. Also missing from that zip file is the Google.Apis.Samples.Helper classes.

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

Does anyone have some useful example code for interacting with YouTube using the latest NuGet code as of Jan 14, 2014?

推荐答案

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

So after much research, digging and a little less hair, I figured out a few things.

首先,登录Google Cloud Console".如果您使用 GAE (Google App Engine) 并单击您的 GAE 项目并启用YouTube 数据 API v3",则保证您无处可去!相反,退出您的 GAE 项目,并创建一个名为API 项目"的新项目,例如.

First, log into the "Google Cloud Console". If you're using GAE (Google App Engine) and you click on your GAE project and enable the "YouTube Data API v3", you are guaranteed to get NO WHERE! Instead, back out of your GAE project, and create a new Project called "API Project" for example.

然后在那个项目中,启用您想要的 API,您将开始获得更好的结果.结果要好得多.首先尝试 YouTube 搜索.这允许您只插入 API 密钥,而不必与 OAuth2 混淆,并且它需要的 dll 更少,因此它是一个很好的起点.请尝试以下操作:

Then within that project, enable your desired API's and you'll begin to get better results. Much better results. Start first with trying a YouTube search. This allows you to just insert your API key and you don't have to mess with OAuth2 and it requires less dll's, so its a good place to start. Try something like the following:

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.

Feel free to replace CommandLine with regular Console print stmts.

接下来,继续使用 OAuth 2.0 并尝试让您的凭据顺利通过.您需要从凭据"部分下的Google Cloud Console"下载 OAuth JSON 文件.获得此文件后,将所有名为client_secrets.json"的文件替换为下载的 json 文件的内容.为了获得工作授权,我发现我缺少 Microsoft.Threading.Tasks.Extensions.Desktop.dll,它是允许浏览器打开一个窗口以授予本机应用程序访问权限的 dllYouTube 帐户.因此,如果您在授权部分遇到一些错误,请检查内部异常,这也可能是您的问题.

Next, move on to OAuth 2.0 and try to get your credentials to go through without erroring. You'll need to download your OAuth JSON file from "Google Cloud Console" under the "Credentials" section. Once you have this file, replace any files named "client_secrets.json" with the contents of the downloaded json file. In order to get the authorization to work, I found that I was missing the Microsoft.Threading.Tasks.Extensions.Desktop.dll which is the dll that allows the browser to open a window to grant access for the Native Application to muck with your YouTube acct. So if you have some errors during the Authorization part, check the inner exception and there's a chance that might be your issue as well.

免责声明:下面显示的代码的下半部分来自:github.com/youtube/api-samples/blob/master/dotnet

Disclaimer: The bottom half of the code shown below was snarfed from: 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 的任何使用"调用替换为使用 DotNetOpenAuth".

So there's my 2 cents worth. Also, you'll need to do a NuGet on DotNetOpenAuth and within your code, replace any "using" calls to Google.Apis.Auth.OAuth2.DotNetOpenAuth to just "using DotNetOpenAuth".

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

Hopefully this helps others. The big thing was figuring out the GAE versus a new project. Once I figured that out, normal amounts of research started yielding results rather than pure frustration!!

这篇关于使用 C# 和 Google.Apis.YouTube.v3 列出 YouTube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆