【问题标题】:YouTube Data API - Creating a live broadcast - getting [liveStreamingNotEnabled] - But it isYouTube 数据 API - 创建直播 - 获取 [liveStreamingNotEnabled] - 但它是
【发布时间】:2015-06-11 08:24:57
【问题描述】:

我正在尝试以编程方式在 YouTube 上创建直播,但出现错误。我创建了一个服务帐户(将从 Web 服务器应用程序在后台运行)。当我尝试执行直播插入请求时,我收到错误“用户未启用直播。[403]”。

但问题是,该帐户的唯一“用户”是我,并且我已启用直播。我假设此权限将由执行 api 调用的服务帐户共享。如果不是这种情况,如何为服务帐户用户启用直播?

这是我的代码。任何帮助将不胜感激。

String serviceAccountEmail = "XXX";

var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
   new ServiceAccountCredential.Initializer(serviceAccountEmail)
   {
       Scopes = new[] { 
           YouTubeService.Scope.Youtube
       }
   }.FromCertificate(certificate));

// Create the service.
var service = new YouTubeService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "My App",
});

LiveBroadcast broadcast = new LiveBroadcast();
LiveBroadcastSnippet snippet = new LiveBroadcastSnippet();

snippet.ScheduledStartTime = new DateTime(2015, 6, 1, 14, 25, 0);
snippet.ScheduledEndTime = new DateTime(2015, 6, 1, 15, 25, 0);
snippet.Title = "Test Event";
broadcast.Kind = "youtube#liveBroadcast";

broadcast.Snippet = snippet;

LiveBroadcastStatus status = new LiveBroadcastStatus();
status.PrivacyStatus = "unlisted";

broadcast.Status = status;

LiveBroadcastsResource.InsertRequest request = service.LiveBroadcasts.Insert(broadcast, "snippet,status");

broadcast = request.Execute();

【问题讨论】:

  • 你想从什么流式传输? “以编程方式”是什么意思?
  • 在能够流式传输之前,您需要创建一个直播。我想使用代码(如上)创建直播。这是针对将有数百个直播流的活动,其中许多将同时运行。

标签: c# google-api youtube-data-api google-api-dotnet-client service-accounts


【解决方案1】:

您目前无法通过 YouTube API 使用服务帐户。我建议您尝试使用 Oauth2 对其进行一次身份验证,然后使用您从身份验证中获得的 refreshtoken 用于所有其他调用。

让您入门的小代码:

/// <summary>
        /// Authenticate to Google Using Oauth2
        /// Documentation https://developers.google.com/accounts/docs/OAuth2
        /// </summary>
        /// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
        /// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
        /// <param name="userName">A string used to identify a user.</param>
        /// <returns></returns>
        public static YouTubeService AuthenticateOauth(string clientId, string clientSecret, string userName)
        {

            string[] scopes = new string[] { YouTubeService.Scope.Youtube,  // view and manage your YouTube account
                                             YouTubeService.Scope.YoutubeForceSsl,
                                             YouTubeService.Scope.Youtubepartner,
                                             YouTubeService.Scope.YoutubepartnerChannelAudit,
                                             YouTubeService.Scope.YoutubeReadonly,
                                             YouTubeService.Scope.YoutubeUpload}; 

            try
            {
                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , userName
                                                                                             , CancellationToken.None
                                                                                             , new FileDataStore("Daimto.YouTube.Auth.Store")).Result;

                YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "YouTube Data API Sample",
                });
                return service;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                return null;

            }

        }

YouTube sample project 中提取的代码。

问题请求:Issue 5370: Youtube v3 Google Service Account Access

【讨论】:

  • 谢谢!几个小时以来一直在努力解决这个问题!
猜你喜欢
  • 2017-05-10
  • 2022-11-07
  • 2015-08-01
  • 2017-07-31
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 2019-04-20
  • 2016-04-20
相关资源
最近更新 更多