对不起,英语不好。 :) 我想使用Windows IoT制作rasberry pi spotify播放器,并使用Spotify Web播放SDK。 但是首先,我必须从spotify api授权代码中获取一个令牌。 为了获得授权码,我必须先登录才能进行Spotify。 我正在尝试模拟发送到Spotify服务器的数据包。但是我总是得到答案“ {\“ error \”:\“ errorInvalidCredentials \”}“ 我在做什么错你能帮忙吗?
这是我的代码:
public class SpotifyApi
{
string clientId = "------------------------";
string clientSecret = "--------------------";
string redirectUri = "https://localhost:8888";
string authorizeUrl = "https://accounts.spotify.com/authorize/?client_id=-------------------&response_type=code&redirect_uri=https://localhost:8888";
string loginUrl = "https://accounts.spotify.com/api/login";
string csrf_token = "";
public async Task<string> Authorize()
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(authorizeUrl);
foreach (var header in response.Headers)
{
if (header.Key == "Set-Cookie")
{
var key = header.Value.ToArray();
csrf_token = key[0].Split(';')[0].Split('=')[1];
IEnumerable<KeyValuePair<string, string>> loginValues = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("csrf_token",csrf_token),
new KeyValuePair<string, string>("username","----------"),
new KeyValuePair<string, string>("password","-----------"),
};
HttpContent content = new FormUrlEncodedContent(loginValues);
client.DefaultRequestHeaders.Referrer = new Uri("https://accounts.spotify.com/en/login?continue=" + authorizeUrl);
var rezult = await client.PostAsync(loginUrl, content);
string rezultString = await rezult.Content.ReadAsStringAsync();
}
}
return "";
}
}