我尝试以编程方式在YouTube上创建直播,但我收到了错误消息。我已经创建了一个服务帐户(将在后台从Web服务器应用程序运行)。当我尝试执行livebroadcast插入请求时,我收到错误"用户未启用实时流式传输。 [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();
答案 0 :(得分: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中删除。