c# - 在工作进程 C# 上验证 Youtube API

标签 c# process youtube youtube-api

我正在运行一个没有 UI 的进程,该进程从 youtube 检索上传的视频。在开发机器上,它代表用户进行身份验证并且可以正常工作。

问题是当进程在服务器上运行时:浏览器窗口不会触发用户让他使用他的帐户(它应该只需要执行一次)。

事件查看器没有显示任何错误。服务帐户似乎不适用于 youtube,API key 的权限太少并且
誓言是验证和获取播放视频的唯一方法。还是我错了?

所以问题是:如何以单个用户的身份运行服务并在没有 UI 的情况下检索他的视频?

         private async Task Run()
                {

                    try
                    {
                        UserCredential credential;
                        using (var stream = new FileStream(StartPath + "\\client_secrets.json", FileMode.Open, FileAccess.Read))
                        {

                            credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                                GoogleClientSecrets.Load(stream).Secrets,
                                new[] { YouTubeService.Scope.YoutubeReadonly, YouTubeService.Scope.Youtube  },
                                "user",
                                CancellationToken.None,
                                new FileDataStore("Store")
                            );
                        }

                        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                        {
                            HttpClientInitializer = credential,
                            ApplicationName = this.GetType().ToString()
                        });
    ....
                 }

我也试过:
 String serviceAccountEmail = "e-mail";

            var certificate = new X509Certificate2(HostingEnvironment.MapPath("~/Content/key.p12"), "notasecret", X509KeyStorageFlags.Exportable);

            ServiceAccountCredential credential = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer(serviceAccountEmail)
              {
                   Scopes = new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubepartnerChannelAudit, YouTubeService.Scope.YoutubeUpload }
               }.FromCertificate(certificate));
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "api",
            });

最佳答案

YouTube 数据 API 可让您将通常在 YouTube 网站上执行的功能整合到您自己的网站或应用程序中。下面的列表确定了您可以使用 API 检索的不同类型的资源。 API 还支持插入、更新或删除其中许多资源的方法。

本引用指南解释了如何使用 API 来执行所有这些操作。该指南按资源类型组织。资源代表构成 YouTube 体验的一部分的项目类型,例如视频、播放列表或订阅。对于每种资源类型,该指南列出了一个或多个数据表示,并且资源表示为 JSON 对象。该指南还列出了一种或多种支持的方法(LIST, POST, DELETE, etc.)对于每种资源类型,并解释如何在您的应用程序中使用这些方法。

以下要求适用于 YouTube 数据 API 请求:

  • 每个请求都必须指定 API key (使用 key 参数)或提供 OAuth 2.0 token 。
    您的 API key 在 Developer Console's 中可用 API 访问 您的项目的 Pane 。
  • 必须为每个插入、更新和删除请求发送一个授权 token 。您还必须为检索经过身份验证的用户的私有(private)数据的任何请求发送授权 token 。

  • 此外,一些用于检索资源的 API 方法可能支持需要授权的参数,或者在请求被授权时可能包含额外的元数据。例如,如果请求由特定用户授权,则检索用户上传视频的请求也可能包含私有(private)视频。
  • API 支持 OAuth 2.0 身份验证协议(protocol)。您可以通过以下任一方式提供 OAuth 2.0 token :
  • 使用 access_token像这样的查询参数:? access_token=oauth2-token
  • 使用 HTTP Authorization像这样的标题:Authorization: Bearer oauth2-token在您的应用程序中实现 OAuth 2.0 身份验证的完整说明可在 authentication guide 中找到。 .
  • 关于c# - 在工作进程 C# 上验证 Youtube API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284672/

    相关文章:

    C# 系统参数异常 : Destination array was not long enough

    c# - 学习什么是一种很好的编程语言来拓宽 C# 开发人员的思维?

    objective-c - 如何在 OS X 的 objective-c 代码中使用 "nettop"获取网络统计信息?

    java - Runtime.exec() 是否允许用户输入?

    ios - 构建 YouTube 应用程序 (Webapp)

    python - 如何抓取 YouTube channel 创建者并链接到他们的 channel ?

    c# - 如何将新创建的用户登录到站点

    c# - 为什么 ViewBag.SomeProperty 在属性不存在时不抛出异常?

    c# - 文件.删除错误 "The process cannot access the file because it is being used by another process"

    reactjs - 无法嵌入播放YouTube视频