2016-01-11 45 views
-6

我正在嘗試使用ASP.NET MVC 4和C#構建一個網站,以使用Spotify web api搜索音樂藝術家的前5個專輯。如何從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,但它並不能幫助我。

+6

爲何不看看Spotify的API文檔? https://developer.spotify.com/web-api/endpoint-reference/ –

+0

我沒有...它沒有幫助。 – zachboy82

+0

你提到你閱讀文檔,但它沒有幫助,因爲它「*沒有解釋足夠的細節*」。如果是這樣的話,爲什麼不問一個關於你不明白的問題*特別是*? –

回答

0

你應該看到這也許對您有所幫助

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; 
       } 
      } 
     } 
    } 
} 
相關問題