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

查看:99
本文介绍了使用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/列表

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

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

  • using Google.Apis.Auth.OAuth2.DotNetOpenAuth;
  • using Google.Apis.Samples.Helper;
  • using Google.Apis.Auth.OAuth2.DotNetOpenAuth;
  • using Google.Apis.Samples.Helper;

然后在代码顶部有以下注释,但是这些链接对我没有什么帮助.

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

/* External dependencies, OAuth 2.0 support, and core client libraries are at: */ /* https://code.google.com/p/google-api-dotnet-client/wiki/APIs#YouTube_Data_API */ /* Also see the Samples.zip file for the Google.Apis.Samples.Helper classes at: */ /* 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 Data API v3",那么您肯定不会在任何地方获得!而是,退出您的GAE项目,并创建一个名为"API Project"的新项目.

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,它是允许浏览器打开一个窗口来授予对本机应用程序的访问权的dll,以破坏您的本地应用程序. YouTube帐户因此,如果您在授权"部分中遇到一些错误,请检查内部异常,并有可能也是您的问题.

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天全站免登陆