Spotify 搜索栏 api,允许用户搜索艺术家并接收艺术家排名前 5 的专辑
Posted
技术标签:
【中文标题】Spotify 搜索栏 api,允许用户搜索艺术家并接收艺术家排名前 5 的专辑【英文标题】:Spotify search bar api that allows a user to search for artist and receives the artists top 5 albums 【发布时间】:2018-07-31 03:39:56 【问题描述】:我现在在单击网页上的按钮时收到错误消息。
System.Net.WebException
The remote server returned an error: (400) Bad Request.
Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): System.
using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Task_3
public partial class Default : System.Web.UI.Page
public void button1Clicked(object sender, EventArgs args)
button1.Text = "Search";
string[] hello = "Hello, World";
MainClass.Main(hello);
MainClass.SetInput(search1.Text);
TableRow tRow = new TableRow();
TableRow tRow2 = new TableRow();
TableCell album1 = new TableCell();
TableCell artist1 = new TableCell();
TableCell date1 = new TableCell();
TableCell tracks1 = new TableCell();
TableCell popularity1 = new TableCell();
TableCell id1 = new TableCell();
TableCell album2 = new TableCell();
TableCell artist2 = new TableCell();
TableCell date2 = new TableCell();
TableCell tracks2 = new TableCell();
TableCell popularity2 = new TableCell();
TableCell id2 = new TableCell();
album1.Text = "Album Name";
artist1.Text = "Artist Name";
date1.Text = "Date of Release";
tracks1.Text = "Number of Tracks";
popularity1.Text = "Popularity";
id1.Text = "ID";
tRow.Cells.Add(album1);
tRow.Cells.Add(artist1);
tRow.Cells.Add(date1);
tRow.Cells.Add(tracks1);
tRow.Cells.Add(popularity1);
tRow.Cells.Add(id1);
Table1.Rows.Add(tRow);
tRow2.Cells.Add(album2);
tRow2.Cells.Add(artist2);
tRow2.Cells.Add(date2);
tRow2.Cells.Add(tracks2);
tRow2.Cells.Add(popularity2);
tRow2.Cells.Add(id2);
Table1.Rows.Add(tRow2);
album1.BorderStyle = BorderStyle.Solid;
artist1.BorderStyle = BorderStyle.Solid;
date1.BorderStyle = BorderStyle.Solid;
tracks1.BorderStyle = BorderStyle.Solid;
popularity1.BorderStyle = BorderStyle.Solid;
id1.BorderStyle = BorderStyle.Solid;
Table1.BorderStyle = BorderStyle.Solid;
Table1.Visible = true;
id2.Text = MainClass.getData(2);
class MainClass
static string input;
static string [] data = new string[25];
public static void fillData()
for (int j = 0; j < 25; j++)
data[j] = "" + j.ToString();
public static string getData(int j)
// fillData();
return data[j];
// return input;
public static void SetInput(string str)
input = str;
public string GetInput()
return input;
public static void Main(string[] args)
fillData();
string search = input;//"Muse";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.spotify.com/v1/search?q=" + search + "&type=artist");
request.Method = "GET";
request.ContentType = "application/json";
request.Accept = "application/json";
request.Headers.Add("Authorization", "Bearer " + "XXXXXXXX");
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // causing the error on website
string myResponse = "";
using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
myResponse = sr.ReadToEnd();
sr.Close();
response.Close();
string[] idStrings = new string[50];
int index1;
for (int i = 0, startIndex = 0; i < 50; i++)
idStrings[i] = "";
index1 = myResponse.IndexOf("id\" : \"", startIndex);
if (index1 == -1) break;
else idStrings[i] = myResponse.Substring(index1 + 7, 22);
startIndex = index1 + 30;
string id = "4aawyAB9vmq...";
HttpWebRequest idRequest = (HttpWebRequest)WebRequest.Create("https://api.spotify.com/v1/albums/" + id);
idRequest.Method = "GET";
idRequest.ContentType = "application/json";
idRequest.Accept = "application/json";
idRequest.Headers.Add("Authorization", "Bearer " + "XXXXXXXXXXX");
HttpWebResponse idResponse = (HttpWebResponse)idRequest.GetResponse();
string myIdResponse = "";
using (System.IO.StreamReader sr = new System.IO.StreamReader(idResponse.GetResponseStream()))
myIdResponse = sr.ReadToEnd();
Console.WriteLine(myResponse.ToString());
for (int i = 0; i < 50; i++)
Console.WriteLine(idStrings[i]);
我在这里做错了什么?我评论了 HTTP 500.Error 处理请求在哪里,但是我不知道如何修复它。该代码通过终端运行,但不能通过网页运行...???
我可能对此写了很多,可能有更简单的方法,但是我之前没有在 asp.net 环境中工作过,所以我是新手。
任何帮助将不胜感激!
另外,如果有人能告诉我下一步如何获取输入并从搜索的艺术家结果中获取 id,这也会有所帮助!
【问题讨论】:
【参考方案1】:您的按钮没有绑定 Onclick 事件,因此单击它永远不会触发 button1Clicked
。
<asp:Button id="button1" runat="server" Text="Search" OnClick="button1Clicked" />
public static void Main(string[] args)
这一行没有引用(GetClientCredentialsAuthToken
也是如此),看起来像 Winform 方法。你确定你没有混用 WEBforms 和 WINforms 代码吗?
【讨论】:
谢谢!我对上面的代码进行了更改,并将数据放入表中。现在,当我传入 main 时,我在终端中工作的代码在网页上不起作用。 首先,Console.WriteLine
仍然是 WINforms 代码。当我试图运行你的代码时,它会在HttpWebResponse response = (HttpWebResponse)request.GetResponse()
上给出(401) Unauthorized
。这意味着您无权提出该请求。您提供了错误的凭据或使用了错误的授权类型(如果它们是真实的,我已经掩盖了上述不记名代码以防止滥用)以上是关于Spotify 搜索栏 api,允许用户搜索艺术家并接收艺术家排名前 5 的专辑的主要内容,如果未能解决你的问题,请参考以下文章
在 Spotify 的搜索 API 中按艺术家搜索的正确 python 语法?