我想在 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/