我正在尝试使用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#进行身份验证然后检索信息的信息。
编辑:我应该提一下,我是这个东西的绝对初学者,虽然我确实阅读了spotify api参考,但它没有帮助我,因为它没有解释足够的细节。
例如,web api用户指南讨论了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,这对我没有帮助。
答案 0 :(得分:0)
你应该看到这对你有帮助
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;
}
}
}
}
}