在 spotify 上收集令牌

Collect token on spotify

我喜欢在 Spotify api 上收集令牌,但程序 return“Id = 356,状态 = WaitingForActivation,方法 =“{null}”,结果 =“{尚未计算}” ”。我不知道,如果我做坏事,我正在帮助

代码:

private void code()
{
    var ds = $"https://accounts.spotify.com/authorize?client_id={Properties.Resources.clientidSp}&response_type=code&redirect_uri={url}&show_dialog=true&scope=user-read-private%20user-read-email%20user-modify-playback-state%20user-read-playback-position%20user-library-read%20streaming%20user-read-playback-state%20user-read-recently-played%20playlist-read-private";

    Process.Start(ds);
}

static HttpListener _httpListener = new HttpListener();
private async void Form1_Load(object sender, EventArgs e)
{
    _httpListener.Prefixes.Add("http://localhost:5000/"); 
    _httpListener.Start(); // start server (Run application as Administrator!)
    Console.WriteLine("Server started.");
    Thread _responseThread = new Thread(ResponseThread);
    _responseThread.Start(); // start the response thread
    code();
}


static void ResponseThread()
{
    HttpListenerContext context = _httpListener.GetContext()
    var client = new RestClient("https://accounts.spotify.com/api/token");
    var request = new RestRequest("POST");
    request.AddParameter("grant_type", "authorization_code");
    request.AddParameter("client_secret", Properties.Resources.clientSecredtSp);
    request.AddParameter("client_id", Properties.Resources.clientidSp);
    request.AddParameter("code", context.Request.RawUrl.Replace("/?code=", ""));
    request.AddParameter("redirect_uri", "http://localhost:5000/");

    var responses = client.ExecuteAsync(request);
}

我需要在我的代码中添加什么来收集令牌?? 谢谢你支持我!!

您正在调用没有等待的异步方法,请尝试将您的代码更改为此(至少)...

static async Task ResponseThread()
{
    HttpListenerContext context = _httpListener.GetContext()
    var client = new RestClient("https://accounts.spotify.com/api/token");
    var request = new RestRequest("POST");
    request.AddParameter("grant_type", "authorization_code");
    request.AddParameter("client_secret", Properties.Resources.clientSecredtSp);
    request.AddParameter("client_id", Properties.Resources.clientidSp);
    request.AddParameter("code", context.Request.RawUrl.Replace("/?code=", ""));
    request.AddParameter("redirect_uri", "http://localhost:5000/");

    var responses = await client.ExecuteAsync(request);
}

...注意我修改的第一行和倒数第二行。