您目前无法在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示例项目 。
发出请求: 问题5370:Youtube v3 Google服务帐户访问权限