YouTube 数据 API - 创建直播 - 获取 [liveStreamingNotEnabled] - 但它是

YouTube Data API - Creating a live broadcast - getting [liveStreamingNotEnabled] - But it is

我正在尝试以编程方式在 YouTube 上创建实时流,但出现错误。我已经创建了一个服务帐户(将在 Web 服务器应用程序的后台 运行)。当我尝试执行直播插入请求时,出现错误 "The user is not enabled for live streaming. [403]".

但事情是这样的,该帐户中唯一的 "user" 是我,我已启用直播。我假设此权限将由执行 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();

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