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

use*_*160 5 c# google-api youtube.net-api

我正在尝试使用最新版本的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下载中找到...

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

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

/* 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代码库,这些代码库与人们似乎开始工作的示例相吻合.

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

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

自2014年1月14日起,是否有人使用最新的NuGet代码与YouTube进行交互?

use*_*160 17

经过大量的研究,挖掘和少量头发,我想出了一些东西.

首先,登录"Google Cloud Console".如果您使用的是GAE(Google App Engine)并且点击了GAE项目并启用了"YouTube Data API v3",那么您可以保证不会在任何地方获得!相反,退出GAE项目,并创建一个名为"API项目"的新项目.

然后在项目中,启用所需的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);
}
Run Code Online (Sandbox Code Playgroud)

随意用常规的Console打印stmts替换CommandLine.

接下来,转到OAuth 2.0并尝试获取您的凭据,而不会出错.您需要从"凭据"部分下的"Google Cloud Console"下载OAuth JSON文件.获得此文件后,将名为"client_secrets.json"的所有文件替换为下载的json文件的内容.为了获得授权,我发现我错过了Microsoft.Threading.Tasks.Extensions.Desktop.dll这个dll,它允许浏览器打开一个窗口来授予Native Application访问权限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();
}
Run Code Online (Sandbox Code Playgroud)

所以我的价值是2美分.此外,您需要在DotNetOpenAuth上和您的代码中进行NuGet,将对Google.Apis.Auth.OAuth2.DotNetOpenAuth的任何"使用"调用替换为"使用DotNetOpenAuth".

希望这有助于其他人.最重要的是找出GAE与新项目.一旦我发现了这一点,正常数量的研究开始产生结果而不是纯粹的挫折!