如何通过 C# 使用 YouTube 数据 API 获取视频状态
How to get video status using YouTube Data API with C#
我已在 3 天内尝试使用代码获取我的 YouTube 频道中的视频状态信息,但没有成功。
下面是我使用的代码,我可以writeln()
视频的标题,id,描述,但是不能获取视频状态。
private async Task Editvideo()
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows for read-only access to the authenticated
// user's account, but not other types of account access.
new[] { YouTubeService.Scope.YoutubeReadonly },
"user",
CancellationToken.None,
new FileDataStore(this.GetType().ToString())
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
var channelsListRequest = youtubeService.Channels.List("contentDetails");
channelsListRequest.Mine = true;
// Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
var channelsListResponse = await channelsListRequest.ExecuteAsync();
foreach (var channel in channelsListResponse.Items)
{
// From the API response, extract the playlist ID that identifies the list
// of videos uploaded to the authenticated user's channel.
var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
Console.WriteLine("Videos in list {0}", uploadsListId);
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;
// Retrieve the list of videos uploaded to the authenticated user's channel.
var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
foreach (var playlistItem in playlistItemsListResponse.Items)
{
// Print information about each video.
// playlistItem.Status = new PlaylistItemStatus();
// string videostatus = playlistItem.Status.PrivacyStatus;
// Console.WriteLine(videostatus);
Console.WriteLine("{0} ({1})", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
}
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
}
如果考虑到 PlaylistItems.list
API 端点的 part
请求参数的规范,您的问题的答案是立即的:
part
(string)
The part
parameter specifies a comma-separated list of one or more playlistItem
resource properties that the API response will include.
If the parameter identifies a property that contains child properties, the child properties will be included in the response. For example, in a playlistItem
resource, the snippet
property contains numerous fields, including the title
, description
, position
, and resourceId
properties. As such, if you set part=snippet
, the API response will contain all of those properties.
The following list contains the part
names that you can include in the parameter value:
contentDetails
id
snippet
status
现在,很明显,当端点需要返回 status
属性 时,part
也应该包含 status
,如下所示:
var playlistItemsListRequest = youtubeService
.PlaylistItems.List("snippet,status");
通过对代码的这种更改,从 API 获得的响应将包括 playlistItem.Status
下的查询对象。
我已在 3 天内尝试使用代码获取我的 YouTube 频道中的视频状态信息,但没有成功。
下面是我使用的代码,我可以writeln()
视频的标题,id,描述,但是不能获取视频状态。
private async Task Editvideo()
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows for read-only access to the authenticated
// user's account, but not other types of account access.
new[] { YouTubeService.Scope.YoutubeReadonly },
"user",
CancellationToken.None,
new FileDataStore(this.GetType().ToString())
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
var channelsListRequest = youtubeService.Channels.List("contentDetails");
channelsListRequest.Mine = true;
// Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
var channelsListResponse = await channelsListRequest.ExecuteAsync();
foreach (var channel in channelsListResponse.Items)
{
// From the API response, extract the playlist ID that identifies the list
// of videos uploaded to the authenticated user's channel.
var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
Console.WriteLine("Videos in list {0}", uploadsListId);
var nextPageToken = "";
while (nextPageToken != null)
{
var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = uploadsListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;
// Retrieve the list of videos uploaded to the authenticated user's channel.
var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();
foreach (var playlistItem in playlistItemsListResponse.Items)
{
// Print information about each video.
// playlistItem.Status = new PlaylistItemStatus();
// string videostatus = playlistItem.Status.PrivacyStatus;
// Console.WriteLine(videostatus);
Console.WriteLine("{0} ({1})", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
}
nextPageToken = playlistItemsListResponse.NextPageToken;
}
}
}
如果考虑到 PlaylistItems.list
API 端点的 part
请求参数的规范,您的问题的答案是立即的:
part
(string)The
part
parameter specifies a comma-separated list of one or moreplaylistItem
resource properties that the API response will include.If the parameter identifies a property that contains child properties, the child properties will be included in the response. For example, in a
playlistItem
resource, thesnippet
property contains numerous fields, including thetitle
,description
,position
, andresourceId
properties. As such, if you setpart=snippet
, the API response will contain all of those properties.The following list contains the
part
names that you can include in the parameter value:
contentDetails
id
snippet
status
现在,很明显,当端点需要返回 status
属性 时,part
也应该包含 status
,如下所示:
var playlistItemsListRequest = youtubeService
.PlaylistItems.List("snippet,status");
通过对代码的这种更改,从 API 获得的响应将包括 playlistItem.Status
下的查询对象。