Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private async Task<string> GetAccessToken()
{
SpotifyToken token = new SpotifyToken();
string postString = string.Format("grant_type=client_credentials");
byte[] byteArray = Encoding.UTF8.GetBytes(postString);
string url = "https://accounts.spotify.com/api/token";
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("Authorization", "Basic {Encoded ClientId:ClientSecret}"); request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse response = await request.GetResponseAsync())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(responseStream))
{
string responseFromServer = reader.ReadToEnd();
token = JsonConvert.DeserializeObject<<strong>SpotifyToken</strong>>(responseFromServer);
}
}
}
}
return token.access_token;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment