【问题标题】:Getting the user's "Favorites" playlist: contentDetails.relatedPlaylists.favorites is an invalid ID获取用户的“收藏”播放列表:contentDetails.relatedPlaylists.favorites 是无效 ID
【发布时间】:2017-12-08 14:42:38
【问题描述】:

Google documentation on this feature 很清楚,除了这不起作用(我使用的是 C#,但由于 API 本身在不同语言中是相同的,希望这将是可以理解的

private async Task FetchYtUploads()
        {
            UserCredential credential;
            var credentialsPath = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), @"..\..\..\..\client_id.json").Replace("file:\\", "");
            using (var stream = new FileStream(credentialsPath, 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;
            var channels = await channelsListRequest.ExecuteAsync(); // A list with just one item is returned

            if (channels.Items.Count <= 0)
            {
                Console.Write("ERROR: no YT channels found.");
                return;
            }

            var favoritesListId = channels.Items[0].ContentDetails.RelatedPlaylists.Favorites; 

channels.Items 是仅包含一项的列表。 channels.Items[0].ContentDetails.RelatedPlaylists.Favorites 有一个 ID,但它与我的“收藏夹”的 URL 链接中的 ID 不同。以下请求失败,ID 为 RelatedPlaylists.Favorites,但如果我对在列表 URL 中查找的 ID 进行硬编码,则可以:

var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
playlistItemsListRequest.PlaylistId = favoritesListId;
playlistItemsListRequest.MaxResults = 50;
playlistItemsListRequest.PageToken = nextPageToken;

var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();

发生了什么事?我错过了什么吗?如何为我的“收藏”播放列表获取正确的 ID,如果不能在 playlistItemsListRequest.PlaylistId 中使用,ContentDetails.RelatedPlaylists.Favorites 是什么?

【问题讨论】:

    标签: c# google-api youtube-api google-api-dotnet-client youtube-data-api


    【解决方案1】:

    您需要记住,YouTube API 是基于频道的。当您使用应用程序向 API 进行身份验证时,系统会要求您选择一个通道。您拥有的身份验证只会让您访问这个频道。

    如果您想访问不同的频道,则必须重新验证您的应用程序并选择不同的频道。

    提示:将“用户”更改为其他内容,它将再次请求身份验证。 user 是您的凭据存储在 %appData%

    下的用户名

    【讨论】:

    • 我想到了。但我确实已经验证了客户端以访问 my 帐户,并且我很肯定我选择了正确的帐户/频道。很奇怪。
    • 换个用户再试一次,你可能选错了。
    • 谢谢先生(或女士),您是对的。我以为我选择了我的两个 YT 频道中正确的一个(不知道我怎么有两个),但它实际上是错误的。
    • 我有六个我一直这样做:)
    猜你喜欢
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 2015-11-21
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多