Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net Youtube API v3:使用服务帐户访问Youtube视频_.net_Youtube Api_Service Accounts_Youtube Data Api - Fatal编程技术网

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

.net Youtube API v3:使用服务帐户访问Youtube视频,.net,youtube-api,service-accounts,youtube-data-api,.net,Youtube Api,Service Accounts,Youtube Data Api,我正在尝试使用Google服务帐户在.NET中列出来自YouTube数据API v3的视频。我已经激活了YouTube API,创建了帐户服务,但每当我试图从我的频道列出视频时,它会显示0个视频,即使我确实上传了视频。你能帮忙吗?谢谢 这是密码- string[] scopes = new string[] { YouTubeAnalyticsService.Scope.YtAnalyticsReadonly, YouTubeAnalyticsService.Scop

我正在尝试使用Google服务帐户在.NET中列出来自YouTube数据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;
}
string[]范围=新字符串[]
{    
YouTubeanAnalyticsService.Scope.ytAnalyticsReady,
YouTubeanAnalyticsService.Scope.ytAnalyticsNetaryReadOnly,
"https://www.googleapis.com/auth/youtube.force-ssl" 
};
//服务帐户凭据
GoogleCredential credential=GoogleCredential.FromFile(SecretPath).CreateScoped(scopes);
var youtubeService=new youtubeService(new BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=channelinfo.AnalyticsApifiedataStore
});
Dictionary videoIds=新字典();
var channelResourcesList=youtubeService.Channels.List(“contentDetails”);
channelResourcesList.Id=channelinfo.ChannelId;
var listResult=channelResourcesList.Execute();
string playlaid=listResult.Items[0]?.ContentDetails.RelatedPlaylists.Uploads;
var listOfVideos=youtubeService.playlitems.List(“片段”);
listOfVideos.playlID=播放ID;
var nextPageToken=“”;
while(nextPageToken!=null)
{
listOfVideos.MaxResults=50;
listOfVideos.PageToken=nextPageToken;
//searchresult为空(listOfVideos.Execute()返回空结果),尽管channelResourcesList.Execute()工作正常
var searchListResult=listOfVideos.Execute();
nextPageToken=searchListResult.nextPageToken;
}

YouTube API不支持服务帐户身份验证

如果您注意到他们没有说明如何设置服务帐户,则您链接的页面中会出现错误,因为这很可能是生成的文档。我很幸运地联系了团队,他们会修复它,这样其他开发人员就不会感到困惑了


您需要使用Oauth2并至少对用户进行一次身份验证,然后保存刷新令牌,以便在需要访问API时请求新的访问令牌。

感谢您调查此问题。我已经更新了code.AFAIK,它不支持服务帐户,因为它未与任何YouTube频道关联,并且您无法将新的或现有的频道与服务帐户关联。在YouTube API wiki中,提到“您可以为web应用程序、服务帐户或已安装的应用程序生成OAuth 2.0凭据。”