我如何从Spotify Web API获得艺术家的前5张专辑

本文关键字:艺术家 专辑 5张 API Spotify Web | 更新日期: 2023-09-27 18:30:02

我正在尝试使用ASP.NET MVC 4和C#创建一个网站,使用Spotify web api搜索音乐艺术家的前5张专辑。

这是我到目前为止的代码:

public ActionResult Result(string Artist)
    {
        var httpClient = new HttpClient();
        httpClient.BaseAddress = new Uri("https://api.spotify.com/");
        // what goes here to authenticate and get the info?
        return View();
    }

我找不到告诉我如何验证然后使用C#检索信息的信息。

编辑:我应该提到,我是这方面的绝对初学者,虽然我确实阅读了spotifyAPI参考资料,但它对我没有帮助,因为它没有解释足够的细节。

例如,webapi用户指南谈到了GET、POST等,但没有给出任何代码来帮助解释如何使用它

$ curl -H "Authorization: Basic Yjc...cK" -d grant_type=refresh_token -d refresh_token=AQD...f0 "https://accounts.spotify.com/api/token"
{
    "error": "invalid_client",
    "error_description": "Invalid client secret"
}

这让我很困惑,因为我不知道该把它放在哪里,我不知道代码是什么语言,等等。

我找不到C#的例子。甚至代码示例都是javascript,这对我没有帮助。

我如何从Spotify Web API获得艺术家的前5张专辑

您应该看到这可能会对您的有所帮助

TagSearch

using System;
using System.Net;
using System.Text;
using System.Linq;
using System.Collections.Generic;
namespace TagSearch.Plugins
{
    public class Spotify : IClass
    {
        #region JsonParseData
        public class Rootobject
        {
            public Tracks tracks { get; set; }
        }
        public class Tracks
        {
            public List<Item> items { get; set; }
        }
        public class Item
        {
            public Album album { get; set; }
            public List<Artist> artists { get; set; }
            public int disc_number { get; set; }
            public string name { get; set; }
            public int track_number { get; set; }
        }
        public class Album
        {
            public string href { get; set; }
            public string release_date { get; set; }
            public string release_date_precision { get; set; }
            public List<Image> images { get; set; }
            public string name { get; set; }
        }
        public class Image
        {
            public int height { get; set; }
            public string url { get; set; }
            public int width { get; set; }
        }
        public class Artist
        {
            public string name { get; set; }
        }
        #endregion
        protected Dictionary<String, int> _TempTotal;
        protected Dictionary<String, List<ITag>> _TempTag;
        private object _Lock = new object();
        public Spotify()
        {
            JParse = new System.Text.Json.JsonParser();
            _TempTotal = new Dictionary<string, int>();
            _TempTag = new Dictionary<String, List<ITag>>();
        }
        public override void Search(string Name)
        {
            GetInfo(Name);
        }
        protected override void GetInfo(string Name)
        {
            lock (_Lock)
            {
                _TempTotal.Add(Name, -1);
                _TempTag.Add(Name, new List<ITag>());
            }
            var web = new IWebClient();
            web.DownloadDataCompleted += DownloadDataCompleted;
            web.DownloadDataAsync(new Uri("https://api.spotify.com/v1/search?&type=track&limit=50&q=" + Uri.EscapeDataString(Name.ToLower())), new IWebClient.WebClientState(Name, 1, null));
            while (_TempTotal[Name] != _TempTag[Name].Count) { System.Threading.Thread.Sleep(1000); }
            OnEvent(Name,_TempTag[Name]);
            _TempTotal.Remove(Name);
            _TempTag.Remove(Name);
            base.GetInfo(Name);
        }
        protected void DownloadDataCompleted(dynamic sender, dynamic e)
        {
            if (e.Result != null)
            {
                string Name = e.UserState.Name;
                switch ((int)e.UserState.State)
                {
                    case 1:
                        var _RootObject = JParse.Parse<Rootobject>(Encoding.UTF8.GetString(e.Result));
                        _TempTotal[Name] = _RootObject.tracks.items.Count;
                        foreach (var Json in _RootObject.tracks.items)
                        {
                            var web = new IWebClient();
                            web.DownloadDataCompleted += DownloadDataCompleted;
                            web.DownloadDataAsync(new Uri(Json.album.href), new IWebClient.WebClientState(Name, 2, new ITag(this.GetType(), Json.name, Json.album.name, Json.artists[0].name, null, DateTime.MinValue, Json.disc_number, Json.track_number)));
                            System.Threading.Thread.Sleep(250);
                        }
                        sender.Dispose();
                        break;
                    case 2:
                        var Json2 = JParse.Parse<Album>(System.Text.Encoding.UTF8.GetString(e.Result));
                        e.UserState.State = 3;
                        switch ((string)Json2.release_date_precision)
                        {
                            case "year": e.UserState.Tag.RelaseDate = DateTime.Parse(Json2.release_date + "-01-01"); break;
                            case "month": e.UserState.Tag.RelaseDate = DateTime.Parse(Json2.release_date + "-01"); break;
                            case "day": e.UserState.Tag.RelaseDate = DateTime.Parse(Json2.release_date); break;
                        }
                        sender.DownloadDataAsync(new Uri(Json2.images[0].url), e.UserState);
                        break;
                    case 3:
                        e.UserState.Tag.Cover = e.Result;
                        _TempTag[Name].Add(e.UserState.Tag);
                        sender.Dispose();
                        break;
                }
            }
        }
    }
}