【发布时间】:2025-12-30 13:30:12
【问题描述】:
我正在尝试使用 Google 服务帐户在 .NET 中列出来自 YouTube Data API v3 的视频。我已经激活了 YouTube API,创建了帐户服务,但是每当我尝试列出我频道中的视频时,即使我确实上传了视频,它也会显示 0 个视频。能否请你帮忙 ?谢谢。
这里是代码-
string[] scopes = new string[]
{
YouTubeAnalyticsService.Scope.YtAnalyticsReadonly,
YouTubeAnalyticsService.Scope.YtAnalyticsMonetaryReadonly,
"https://www.googleapis.com/auth/youtube.force-ssl"
};
// service account credential
GoogleCredential credential = GoogleCredential.FromFile(SecretPath).CreateScoped(scopes);
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = channelinfo.AnalyticsAPIFileDataStore
});
Dictionary<string, DateTime?> videoIds = new Dictionary<string, DateTime?>();
var channelResourcesList = youtubeService.Channels.List("contentDetails");
channelResourcesList.Id = channelinfo.ChannelId;
var listResult = channelResourcesList.Execute();
string playListId = listResult.Items[0]?.ContentDetails.RelatedPlaylists.Uploads;
var listOfVideos = youtubeService.PlaylistItems.List("snippet");
listOfVideos.PlaylistId = playListId;
var nextPageToken = "";
while (nextPageToken != null)
{
listOfVideos.MaxResults = 50;
listOfVideos.PageToken = nextPageToken;
// searchresult is empty (listOfVideos.Execute() returns empty result) although channelResourcesList.Execute() worked
var searchListResult = listOfVideos.Execute();
nextPageToken = searchListResult.NextPageToken;
}
【问题讨论】:
-
感谢您调查此问题。我已经更新了代码。
-
AFAIK,Youtube API 不支持服务帐户,因为它未与任何 YouTube 频道相关联,您无法将新的或现有频道与服务帐户相关联。
-
在 youtube API wiki - developers.google.com/youtube/registering_an_application 中提到“您可以为 Web 应用程序、服务帐户或已安装的应用程序生成 OAuth 2.0 凭据。”
标签: .net youtube-api service-accounts youtube-data-api