Home > Blockchain >  Collect token on spotify
Collect token on spotify

Time:02-24

I liked collect token on spotify api, but program return "Id = 356, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"". I dont know, if i do someting bad, I’m helping by this question.

Code:

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 user-read-email user-modify-playback-state user-read-playback-position user-library-read streaming user-read-playback-state user-read-recently-played playlist-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);
}

What i need add on my code to collect token?? Thanks for supporting me!!

CodePudding user response:

You're calling an Async method without the await, try changing your code to this (at a minimum) ...

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);
}

... noting the first and second last lines that I have modified.

  • Related