Ramco Ramco

Ramco Ramco

  • 442
  • 3.4k
  • 517.4k

Token in JsonFormat

Feb 7 2021 7:40 AM
Hi
In below code i want to get token & what is the best option to save that token
  1. public class AccessToken  
  2. {  
  3. public string access_token { getset; }  
  4. public string token_type { getset; }  
  5. public long expires_in { getset; }  
  6. }  
  7. private static async Task<string> GetToken()  
  8. {  
  9. string credentials = String.Format("{0}:{1}", clientId, clientSecret);  
  10. using (var client = new HttpClient())  
  11. {  
  12. client.DefaultRequestHeaders.Accept.Clear();  
  13. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  14. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(credentials)));  
  15. List<KeyValuePair<stringstring>> requestData = new List<KeyValuePair<stringstring>>();  
  16. requestData.Add(new KeyValuePair<stringstring>("grant_type""client_credentials"));  
  17. FormUrlEncodedContent requestBody = new FormUrlEncodedContent(requestData);  
  18. var request = await client.PostAsync("https://accounts.spotify.com/api/token", requestBody);  
  19. var response = await request.Content.ReadAsStringAsync();  
  20. return JsonConvert.DeserializeObject<AccessToken>(response);  
  21. }  
  22. }  
Thanks

Answers (4)