Youtube API v3:使用服务帐户访问 YouTube 视频

Ani*_*tak 6 .net youtube-api service-accounts youtube-data-api

我正在尝试使用 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;
}
Run Code Online (Sandbox Code Playgroud)

DaI*_*mTo 3

更新

In formation on how to configure a service account with use with the YouTube API can be found here. Server sided web apps

As well as a C# example

GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
                {
                    credential = GoogleCredential.FromStream(stream)
                         .CreateScoped(scopes);
                }

                // Create the  YouTubeService service.
                return new YouTubeService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "YouTube Service account Authentication Sample",
                });
Run Code Online (Sandbox Code Playgroud)