尝试刷新Spotify令牌时,我一直遇到500个错误。
错误:{“error”:“server_error”}
请求:
POST https://accounts.spotify.com/api/token HTTP / 1.1 授权:基本ZWM ..... WI = 主持人:accounts.spotify.com 内容长度:170 期待:100-继续 连接:Keep-Alive
grant_type = refresh_token&安培; refresh_token = AQA9 ..... P5QC
代码:
string request = "https://accounts.spotify.com/api/token";
StringBuilder sbOutput = new StringBuilder();
StringBuilder sbBody = new StringBuilder();
sbBody.Append("grant_type=refresh_token");
sbBody.Append("&refresh_token=" + spotifyToken.RefreshToken);
//sbBody.Append("&client_id=" + "ec41.....19");
//sbBody.Append("&client_secret=" + "9b...5b");
string clientIdAndSecret = "ec....19:9ba.....e5b";
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(clientIdAndSecret);
string clientIdAndSecretBase64 = System.Convert.ToBase64String(plainTextBytes);
Dictionary<string, string> headers = new Dictionary<string, string>();
headers["Authorization"] = "Basic " + clientIdAndSecretBase64;
response = SendRequest("POST", request, sbBody.ToString(), headers, ref sbOutput);
SendRequest方法:
if (!string.IsNullOrEmpty(url))
{
using (var client = new HttpClient())
{
//Create request URI
Uri requestURI = new Uri(url);
//Create Stream from the content
Stream bodyStream = new MemoryStream(Encoding.UTF8.GetBytes(body ?? ""));
HttpContent requestContent = new StreamContent(bodyStream);
//Headers
foreach (KeyValuePair<string, string> pair in headers)
{
client.DefaultRequestHeaders.Add(pair.Key, pair.Value);
}
Task<HttpResponseMessage> responseTask;
switch (httpWebRequestMethod.ToUpper())
{
case "POST":
responseTask = client.PostAsync(requestURI, requestContent);
break;
case "GET":
responseTask = client.GetAsync(requestURI);
break;
case "DELETE":
responseTask = client.DeleteAsync(requestURI);
break;
case "PUT":
responseTask = client.PutAsync(requestURI, requestContent);
break;
default:
return HttpStatusCode.Unused;
}
//Lets wait
responseTask.Wait();
if (responseTask.IsCompleted)
{
HttpResponseMessage response = responseTask.Result;
string resultBody = response.Content.ReadAsStringAsync().Result;
sbOutput.Append(resultBody);
return response.StatusCode;
}
}
}
return HttpStatusCode.Unused;
}
任何帮助都会非常感激,因为我已经准备好把头撞到墙上了!
谢谢:)