Skip to content
Permalink
5242d29cff
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
38 lines (26 sloc) 1.18 KB
using System.Text;
using System.Text.Json;
using app.Models;
namespace app.Services;
public class SpotifyService : ISpotifyService {
private string accessToken;
public SpotifyService() {
}
public async Task<bool> Initialize(string authCode) {
var bytes = Encoding.UTF8.GetBytes($"{Constants.SpotifyClientId}:{Constants.SpotifyClientSecret}");
var authHeader = Convert.ToBase64String(bytes);
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", authHeader);
var content = new FormUrlEncodedContent(new List<KeyValuePair<string, string>>() {
new("code", authCode),
new("redirect_uri", Constants.RedirectUrl),
new("grant_type", "authorization_code")
});
var response = await client.PostAsync("https://accounts.spotify.com/api/token", content);
var json = await response.Content.ReadAsStringAsync();
//deserializing token
var result = JsonSerializer.Deserialize<AuthResult>(json);
accessToken = result.AccessToken;
return response.IsSuccessStatusCode;
}
}