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

謝謝!幾個小時以來,我一直對此感到震驚! –