c# - Youtube Data API C# - 无需请求用户凭据即可使用

标签 c# .net youtube youtube-api youtube-data-api

我想在 C# 中使用 YouTubeData API - .Net Core - 而无需请求用户凭据。
我需要从这个 API 中唯一需要的是检索播放列表的信息,但是当我在 localhost 上使用它时,它总是在请求用户的凭据。
如何在没有任何凭据请求或使用我自己的 token 的情况下使用此 API?

最佳答案

如果您想检索公共(public)播放列表的信息(标题、缩略图),您只需要一个 API key 。如果您想创建或编辑播放列表,您只需对用户进行身份验证。

此示例程序按 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)));
        }
    }
}

我希望我能帮助你!

关于c# - Youtube Data API C# - 无需请求用户凭据即可使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61835066/

相关文章:

C# - 使用网络带宽计算下载/上传时间

c# - 隐藏 Windows 8 桌面图标

c# - .CSProj 和 .Sln 文件有什么用?

c# - await 正在停止程序的执行

youtube - ffmpeg 将 jpg 和 mp3 加在一起制作视频上传到 YouTube

c# - 正在录制XAML浏览器应用程序中允许的音频

c# - 涉及锁时,代码契约(Contract)警告 "ensures unproven"

c# - 是否总是从函数返回新列表?

优酷 API : no way to get liked videos from user's activity feed

shell - 如何检查声音是否在我的声卡上停止播放