1

我试图通过编程方式在YouTube上创建实时流,但出现错误。我创建了一个服务帐户(将从Web服务器应用程序的后台运行)。当我尝试执行livebroadcast插入请求时,出现错误“用户未启用实时流媒体。[403]”。YouTube数据API - 创建实况广播 - 获取[liveStreamingNotEnabled] - 但它是

但是,事情就是这样,帐户唯一的“用户”是我,我已启用实时流式传输。我假设这个权限将被执行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(); 
+0

什么是你想流的来源? “以编程方式”是什么意思? –

+0

在能够流式传输之前,您需要创建一个livebroadcast。我想创建使用代码的livebroadcast(如上)。这是一个事件,将有数百个实时流,其中许多将同时运行。 –

回答

0

您目前无法在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

+0

谢谢!几个小时以来,我一直对此感到震惊! –