Youtube Data API C#-在不要求用户证书的情况下使用

问题描述 投票:0回答:1

我想在C#-.Net Core中使用YouTubeData API,而无需请求用户凭据。我唯一需要此API的就是检索播放列表的信息,但是总是在本地主机上使用它时,它正在请求用户的凭据。如何在不要求任何凭证或使用自己的令牌的情况下使用此API?

c# .net youtube youtube-api youtube-data-api
1个回答
0
投票

如果要检索公共播放列表的信息(标题,缩略图),则只需一个API密钥。仅当您要创建或编辑播放列表时,才需要对用户进行身份验证。

此示例程序通过ID检索播放列表的标题和缩略图。

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;

namespace YtbExample
{
    internal class PlayList
    {
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                new PlayList().Run().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
            }

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        private async Task Run()
        {
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = "YOUR_API_KEY",
                ApplicationName = this.GetType().ToString()
            });

            var listRequest = youtubeService.Playlists.List("snippet");
            listRequest.Id = "PLdo4fOcmZ0oXzJ3FC-ApBes-0klFN9kr9";
            //Get playlists by channel id
            //listRequest.ChannelId = "";

            listRequest.MaxResults = 50;


            var listResponse = await listRequest.ExecuteAsync();

            List<string> playlists = new List<string>();

            // Add each result to the list, and then display the lists of
            // matching playlists.
            foreach (var result in listResponse.Items)
            {
                playlists.Add(String.Format("Title: {0}, Id: {1}, Thumbnail: {2}", result.Snippet.Title, result.Id, result.Snippet.Thumbnails.Medium.Url));
            }

            Console.WriteLine(String.Format("{0}\n", string.Join("\n", playlists)));
        }
    }
}

希望我能帮助您!

© www.soinside.com 2019 - 2024. All rights reserved.